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

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.

Related

Plotting with a matrix in Matlab

The thing is i have a matrix and when i use imagesc() it goes like this but my goal is this.
So my question is does any one know which plot is this or some one has document about this, thanks.
If you have two vectors r and theta that give the polar coordinates, or two matrices rGrid and thetaGrid that give the polar coordinates for each element of the data matrix, then code like this will work:
r=linspace(1,20,20);
theta=linspace(0,2*pi,20);
data = r'.*sin(2.*theta); % INSERT DATA HERE
[thetaGrid,rGrid]=meshgrid(theta,r); % Create coordinate grid if needed
[xGrid,yGrid]=pol2cart(thetaGrid,rGrid);
surf(xGrid,yGrid,data); % Plot data
view(2);
Just keep in mind that the rows of the data matrix need to correspond to different radii, and columns need to correspond to different values of theta. If it's flipped, then transpose the matrix before plotting:
data = data';
Also, if the data doesn't wrap around from 0 to 2*pi radians, then repeat the first value of theta as the last value, and repeat the first column of the data matrix as a new final column:
theta(end+1)=theta(1);
data=cat(2,data,data(:,1));
There is also a 3D Polar Plot function on the MATLAB file exchange, but I do not have any experience using it: 3D Polar Plot

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

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

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.

2D color plot using a limited dataset

I couldn't find a solution after trying for a long time.
I have 3 columns of data: x, y, and the stress value (S) at every point (x,y). I want to generate a 2D color plot displaying continuous color change with the magnitude of the stress (S). The stress values increase from -3*10^4 Pa to 4*10^4 Pa. I only have hundreds of data points for an area, but I want to see the stress magnitude (read from the color) at every location (x, y). What Matlab command should I use?
I want to make a 2D color plot showing stress magnitude (S) at every location (x, y) based on continuous color change using limited data points
I'd use patch with interpolated coloring:
% some data, x/y are random
N = 50;
x = rand(N,1);
y = rand(N,1);
S = sin(2*x)+y;
% plotting
tr = delaunay(x,y);
trisurf(tr,x,y,zeros(N,1),S,'FaceColor','interp');
view (2)
Take a look at surf and mesh in the MATLAB documentation
To further contribute on Gunther Struyf answer; assuming it is a FEM analysis you may already have a connectivity matrix say 'M' and 'x' 'y' column vectors with node coordinates. Stress values at the nodes may be contained in a column vector 'S'; then you may use the patch function as stated above:
patch('faces',M,'vertices',[x(:) y(:)],'facevertexcdata',S(:),'FaceColor','interp');
and you will have a 2D plot of your data similar to the one posted by Gunther Struyf.

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.