Surface plot of vectors with x and y vectors as index

7 vues (au cours des 30 derniers jours)
Dane Hylton
Dane Hylton le 24 Oct 2018
Modifié(e) : jonas le 24 Oct 2018
I have 3 vectors xx, yy and zz and I want the surface plot having the x and y axis coordinates as the indexes of the graph (surface plot by convention gives you the row and column indexes as the indexes for the surf plot graph). My zz vector is the z axis.
I can do
plot3(xx,yy,zz)
Which is good, but not exactly what I want. The plot3 function gives me the correct indexes on the graph, but nothing like a surf. I was looking at this example here however, I am getting errors. My code is below, however it is not working and I am getting an out of range error "Out of range subscript." where I call sub2ind.
xx = 31:120;
yy = 1:4:360;
zz = rand(size(xx));
[x,y]=meshgrid(xx,yy);
z=zeros(size(x));
z(sub2ind(size(z),xx,yy))=zz;
surf(x,y,z)

Réponses (1)

jonas
jonas le 24 Oct 2018
Modifié(e) : jonas le 24 Oct 2018
After meshgrid you need to interpolate. For example
z = griddata(xx, yy, zz, x, y)
Then you can plot
surf(x, y, z)
Surf plots are rectangular, always, although you can pad the edges with NaNs to make irregular shapes. The link you refer to use a triangular mesh, which is useful if you want a less regular shape without creating a rectangular grid.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by