Is there a way in matlab to plot several 1D courves on specific coordinates over a 3D mesh? - matlab

For example, I got 3 pairs of 1-D loglog curves and additionally their associated cartesian coordinate points (x,y,z) of one of their ends A, B and C over a mesh surface S (z is positive downwards and linear but coincides in direction with the log(y)-axis from the curves). Is it possible to respresent in a single figure such system of plots in matlab?
Moreover, obtain an interpolated slice from A,B and C?
The images from the question of user3281667 gives an insight of what we are trying to do here:
https://gis.stackexchange.com/questions/252939/interpolating-xyz-data-in-arcgis-3d-analyst
Thanks.

Kind of solved. First we need to know in which format is our data, this case scattered.
I concatenated a nx4 matrix with the preprocessed data A=[X Y Z C].
Then use the right tools, to plot use scatter3: scatter3(A(:,1), A(:,2), A(:,3),30, A(:,4), 'filled' )
Now to interpolate, fisrt generate a grid refinement with meshgrid: [Xm, Ym, Zm] = meshgrid(min(X):2:max(X), min(Y):2:max(Y), min(Z):2:max(Z)) next interpolate using griddata Cm = griddata(X,Y,Z,C,Xm,Ym,Zm);and last plot again.
figure
scatter3(Xm(:), Ym(:), Zm(:), 30, Cm(:), 'filled' )
Thanks to user7431005

Related

i have 100*100 matrix, how can i make plot3 graph?

I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:
I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.
I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!

3d plot with ksdensity in matlab

I have a problem in matlab.
I used a ksdensity function on a vector of deltaX, which was my computed X minus actual X.
And I did the same on deltaY.
Then I used plot on that data. This gave me two 2d plots.
As I have two plots showing how (in)accurate was my system in computing X and Y (something like gaussian bell it was). Now I would like to have one plot but in 3d.
The code was just like that:
[f,xi] = ksdensity(deltaX);
figure;
plot(xi,f)
Ok what I'm about to show is probably not the correct way to visualize your problem, mostly because I'm not quite sure I understand what you're up to. But this will show you an example of how to make the Z matrix as discussed in the comments to your question.
Here's the code:
x = wgn(1000,1,5);%create x and y variables, just noise
y = wgn(1000,1,10);
[f,xi] = ksdensity(x);%compute the ksdensity (no idea if this makes real-world sense)
[f2,xi2] = ksdensity(y);
%create the Z matrix by adding together the densities at each x,y pair
%I doubt this makes real-world sense
for z=1:length(xi)
for zz = 1:length(xi2)
Z(z,zz) = f(z)+f2(zz);
end
end
figure(1)
mesh(xi,xi2,Z)
Here's the result:
I leave it up to you to determine the correct way to visualize your density functions in 3D, this is just how you could make the Z matrix. In short, the Z matrix contains the plot elevation at each x,y coordinate. Hope this helps a little.

How to create a 2D-matrix out of my data for surf()?

I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like:
The fact that your x- and y- values are not integers is not a problem at all. The real question is: are your (x,y) points forming a grid, or not ?
If your points are forming a grid, then you have to reshape your columns to form m-by-n arrays. You may need to sort your data according to the first, then second column and then use the reshape function.
If your points are not forming a grid, then you will have to make an interpolation. By chance the scatterinterpolant class can nicely help you in doing so.
As you can see, the data you are providing is neither given in a gridded way, nor is the point cloud clean. You could however try to do the following:
Project the point cloud onto the x-y plane
Triangulate those points
Give the points their original z-coordinate back.
Plot the surface using trisurf
Here is a MATLAB code that does this:
%// Generate some points P
[X,Y] = ndgrid(0:30);
P = [X(:), Y(:), X(:).^2+Y(:)];
%%// Here the actual computation starts
[~,I] = unique(P(:,1:2),'rows'); %// Remove points with duplicate (x,y)-coords
P = P(I,:);
T = delaunay(P(:,1),P(:,2)); %// Triangulate the 2D-projection
surface = triangulation(T, P(:,1), P(:,2), P(:,3)); %// Project back to 3D
trisurf(surface); %// Plot
You may want to remove stray points first, though.

How to use matlab contourf to draw two-dimensional decision boundary

I finished an SVM training and got data like X, Y. X is the feature matrix only with 2 dimensions, and Y is the classification labels. Because the data is only in two dimensions, so I would like to draw a decision boundary to show the surface of support vectors.
I use contouf in Matlab to do the trick, but really find it hard to understand how to use the function.
I wrote like:
#1 try:
contourf(X);
#2 try:
contourf([X(:,1) X(:,2) Y]);
#3 try:
Z(:,:,1)=X(Y==1,:);
Z(:,:,2)=X(Y==2,:);
contourf(Z);
all these things do not correctly. And I checked the Matlab help files, most of them make Z as a function, so I really do not know how to form the correct Z matrix.
If you're using the svmtrain and svmclassify commands from Bioinformatics Toolbox, you can just use the additional input argument (...'showplot', true), and it will display a scatter plot with a decision boundary and the support vectors highlighted.
If you're using your own SVM, or a third-party tool such as libSVM, what you probably need to do is to:
Create a grid of points in your 2D input feature space using the meshgrid command
Classify those points using your trained SVM
Plot the grid of points and the classifications using contourf.
For example, in kind-of-MATLAB-but-pseudocode, assuming your input features are called X1 and X2:
numPtsInGrid = 100;
x1Range = linspace(x1lower, x1upper, numPtsInGrid);
x2Range = linspace(x2lower, x2upper, numPtsInGrid);
[X1, X2] = meshgrid(x1Range, x2Range);
Z = classifyWithMySVMSomehow([X1(:), X2(:)]);
contourf(X1(:), X2(:), Z(:))
Hope that helps.
I know it's been a while but I will give it a try in case someone else will come up with that issue.
Assume we have a 2D training set so as to train an SVM model, in other words the feature space is a 2D space. We know that a kernel SVM model leads to a score (or decision) function of the form:
f(x) = sumi=1 to N(aiyik(x,xi)) + b
Where N is the number of support vectors, xi is the i -th support vector, ai is the estimated Lagrange multiplier and yi the associated class label. Values(scores) of decision function in way depict the distance of the observation x frοm the decision boundary.
Now assume that for every point (X,Y) in the 2D feature space we can find the corresponding score of the decision function. We can plot the results in the 3D euclidean space, where X corresponds to values of first feature vector f1, Y to values of second feature f2, and Z to the the return of decision function for every point (X,Y). The intersection of this 3D figure with the Z=0 plane gives us the decision boundary into the two-dimensional feature space. In other words, imagine that the decision boundary is formed by the (X,Y) points that have scores equal to 0. Seems logical right?
Now in MATLAB you can easily do that, by first creating a grid in X,Y space:
d = 0.02;
[x1Grid,x2Grid] = meshgrid(minimum_X:d:maximum_X,minimum_Y:d:maximum_Y);
d is selected according to the desired resolution of the grid.
Then for a trained model SVMModel find the scores of every grid's point:
xGrid = [x1Grid(:),x2Grid(:)];
[~,scores] = predict(SVMModel,xGrid);
Finally plot the decision boundary
figure;
contour(x1Grid,x2Grid,reshape(scores(:,2),size(x1Grid)),[0 0],'k');
Contour gives us a 2D graph where information about the 3rd dimension is depicted as solid lines in the 2D plane. These lines implie iso-response values, in other words (X,Y) points with same Z value. In our occasion contour gives us the decision boundary.
Hope I helped to make all that more clear. You can find very useful information and examples in the following links:
MATLAB's example
Representation of decision function in 3D space

MATLAB: Need to make a 4D plot (3D + Colour/Color)

I need to make a 3D surface where colour will represent the fourth variable. I know "surf" is SIMILAR to what I need, but that's not quite it. Basically, I have the following variables:
t = [1:m]
y = [1:n]
a = [1:o]
These should be the three Cartesian corodinate axes.
I also have a variable S that is of dimensions m x n x o, and is basically the amplitude, a function of the previous three variables (i.e. S = f(t,y,a)). I want this to be represented by colour.
So to summarize, I need a graph of the form (t,y,a,S), where the first three variables are vectors of unequal sizes and the final variable is a multidimensional array whose dimensions are determined by the first three.
Thanks in advance.
SCATTER3 requires x, y and z and other grouping arguments to be equally-sized Nx1 vectors for a single series or NxM matrices for M series.
You have full space 3D data. To make equally-sized coordinate vectors use MESHGRID (or NDGRID) function:
[X, Y, Z] = meshgrid(t, y, a);
Then you can use SCATTER3:
scatter3( X(:), Y(:), Z(:), [], S(:) )
The problem is since it's full space data scatter3 will not be helpful specially if you have a lot of points.
You can probably filter your S variable (something like idx = S > 0), then you can plot filtered data.
If you really need to visualize all the data, look at Volume visualization in MATLAB documentation. I can recommend SLICE function, for example.
EDIT
Here is an example of full 3D space scatter plot for small vectors (m, n, o equal to 5) with S = rand([m,n,o]); scatter3( X(:), Y(:), Z(:), [], S(:), 'filled' )
EDIT 2
From your comments to the other answer I found that you have 32x76050x4 matrix. You can actually plot 2D slice one at a time. you can do it in 2D with IMAGESC function, or in 3D with SLICE function.
Try:
imagesc(S(:,:,k))
where k is a number from 1 to 4 for the 3rd dimension.
Or try
slice(S, [], [], 1:size(S,3))
shading flat
Maybe this user-created plotting routine can help.
Screnshot from the linked page:
I've always used scatter3 for coloring/sizing pixels in 3d space. I believe the signature is:
scatter3(x,y,z, size, color)
The size and color can be scalar or vector of length equal to the coordinates. I usually use either the color or the size to reflect the fourth attribute, depending on what I'm showing. I don't have Matlab on this machine, so forgive me if my memory isn't completely accurate on the usage. "help scatter3" should describe it much better.