3D plot matlab with 3 equally sized vectors - matlab

I have three vectors, X, Y, and Z. All of equal length (20000,1). I want to plot all three in a 3d plot. I have tried using surf and plot3 but to no avail as they require Z to be of size (20000,20000). Can anybody help?
TIA
X = DAT(3,:);
Y = DAT(4,:);
Z = DAT(11,:);
[x,y] = meshgrid(X,Y);
surf(x,y,Z);

Have you tried griddata or TriScatteredInterp to create an interpolated surface?

NO! plot3 does NOT require that of Z. If all you wish is to plot a point set, then plot3 does EXACTLY what you want.
plot3(X,Y,Z,'.')
The point is, there is NO need to use meshgrid for plot3. In fact, there is no need to use meshgrid as you have tried in order to use surf. (If you will be calling griddata, then meshgrid would be necessary, but for a SMALLER mesh.)
IF you need a surface plot, then you need to create a surface. If the points are scattered, then your basic options are tools like triscatteredinter, griddata, or gridfit, the last from the file exchange.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/311767 gives the code snippet may help you.
X=rand(1,30);
Y=rand(1,30);
Z=rand(1,30);
[XI YI ZI] = griddata(X,Y,Z,linspace(0,1),linspace(0,1)');
figure
subplot(1,2,1)
trisurf(delaunay(X,Y),X,Y,Z)
subplot(1,2,2);
surf(XI,YI,ZI)

Related

Overlay the data points which make-up a contour plot matrix on the same plot in MATLAB

Hope the title gave an adequate description of my problem. Basically, I am generating a contour plot in MATLAB using the contourf (x,y,z) function, where x and y are vectors of different lengths and z is a matrix of data with dimensions of x times y. The contourf plot is fine, however, I am looking to overlay this plot with the actual data points from the matrix z. I have tried using the scatter function, but I am getting an error message informing me that X and Y must be vectors of the same length - which they're not. Is there any other way to achieve this?
Thanks in advance for any help/suggestions!
I think meshgrid should help you.
z = peaks; %// example 49x49 z data
x = 1:20;
y = 1:49;
z = z(y,x); %// make dimensions not equal so length(x)~=length(y)
[c,h] = contourf(x,y,z);
clabel(c,h); colorbar;
[xx,yy]=meshgrid(x,y); %// this is what you need
hold on;
plot(xx,yy,'k.'); %// overlay points on contourf
Notice plot suffices instead of scatter. If you insist, scatter(xx(:),yy(:),10), for example, does the trick. Although my example isn't particularly interesting, this should hopefully get you started toward whatever you're going for aesthetically.

Magnitude based colored points in Octave or MATLAB or in any other software

I have Z = f(x,y). I have discrete values of Z. I want to get a 2-D plot where the magnitude of Z is represented by the color of the points. The color should vary gradually as the magnitude of Z.
I prefer a Octave or MATLAB solution, but any other software is fine. Any help is much appreciated.
One possible way is to use imagesc:
Example:
Z = rand(10);
imagesc(Z)
You can use surf() along with view in MATLAB. Try this:
figure;
surf(Z);
view(2); % top-down view
You can also enable a color chart that shows color-value correspondence via the colorbar command.
Create a meshgrid with x and y as arrays:
[X,Y]=meshgrid(x,y)
Then use surf to get discrete colored plot with the z values controlling the intensities.
surf(X,Y,z)

isosurface function MATLAB usage

Hi can any one give me a simple example of how to use the isosurface function in MATLAB.
The example given if you type help isosurface is quite confusing. Searching on google did not help as no one gives simple examples anywhere. All of them use predefined functions like
flow.
For starters, suppose i have points (x,y,z) where z=0 and at each point I define a constant
function f(x,y,z)=6. So if I use the isosurface function on the isovalue 6 I would like MATLAB to give me a 3d plot with the XY plane highlighted in some colour, say green.
I don't quite understand your example, but here's how you use isosurface to draw a sphere:
%# create coordinates
[xx,yy,zz] = meshgrid(-15:15,-15:15,-15:15);
%# calculate distance from center of the cube
rr = sqrt(xx.^2 + yy.^2 + zz.^2);
%# create the isosurface by thresholding at a iso-value of 10
isosurface(xx,yy,zz,rr,10);
%# make sure it will look like a sphere
axis equal
The example you gave is very uninteresting, in fact maybe even problematic.
By collapsing all points to z=0, you no longer can/need to use ISOSURFACE, and CONTOUR should be called instead. Even then, a constant function f(X,Y)=6 won't show anything either...
Since #Jonas already showed how to use ISOSURFACE, here is an example for the CONTOUR function:
%# create a function to apply to all X/Y coordinates
[X,Y] = meshgrid(-2:0.1:2,-1:0.1:1);
f = #(X,Y) X.^3 -2*Y.^2 -3*X;
%# plot the function surface
subplot(121), surfc(X,Y,f(X,Y))
axis equal, daspect([1 1 3])
%# plot the iso-contour corresponding to where f=-1
subplot(122), contour(X,Y,f(X,Y),[-1 -1]),
axis square, title('Contour where f(X,Y)=-1')

How to present points on axes in Matlab

I have two vectors of the same length and I'd like to treat the first as X values and the second as Y values and present (x,y) points on a graph.
How do I do this simple thing in Matlab?
Thanks!!!
It seems that it is just plot(x, y) what you need.
For example to plot 'dots' use plot(x, y, '.'). For more examples of markers to use, see the attached link.

How do I make a surf plot in MATLAB with irregularly spaced data?

I know I can create a 3D surface plot in MATLAB by doing:
x = linspace(1,10,100);
y = linspace(10,20,100);
[X Y] = meshgrid(x,y);
Z = X * Y;
surf(X,Y,Z);
But this requires that all the nodes for the height map generated line up. I have a set of data which has arbitrary points (x,y) and a height (z). Is there a simple way to plot a graph which will generate a surface between the points in a similar fashion to surf?
Appologies, after some hunting I managed to answer my own question:
You can use the trisurf function:
tri = delaunay(x,y);
trisurf(tri,x,y,z);
If you have dense data you will want to do shading interp (or another value, check doc shading) so you don't get a black blob due to the grid.
It looks like you've found your answer by using DELAUNAY and TRISURF to generate and plot a triangulated surface.
As an alternative, you could also fit a regularly-spaced grid to your nonuniformly-spaced points in order to generate a surface that can be plotted with the SURF command. I discuss how this can be done using the TriScatteredInterp class (or the deprecated function GRIDDATA) in my answer to this other question on SO.