Surface plot of vectors with x and y vectors as index

10 ビュー (過去 30 日間)
Dane Hylton
Dane Hylton 2018 年 10 月 24 日
編集済み: jonas 2018 年 10 月 24 日
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)

回答 (1 件)

jonas
jonas 2018 年 10 月 24 日
編集済み: jonas 2018 年 10 月 24 日
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