surf indices reversed?
Show older comments
Quick question: It seems surf and surfl have indices reversed. Is this correct? For example, if I type
x=0:pi/10:pi
y = 0:pi/10:2*pi
for i = 1:11
for j = 1:21
z(i,j) = sin(x(i))*sin(y(j))
end
end
surfl(x,y,z)
I get the standard: Error using surfl (line 94)
The lengths of X and Y must match the size of Z.
error. If I use z' in place of z it runs. But shouldn't the first indiex be the x and the second the y? This is completely unintuitive to me.
4 Comments
" It seems surf and surfl have indices reversed. Is this correct? "
Yes.
"But shouldn't the first indiex be the x and the second the y?"
No.
It is due to an inconsistency between mathematical practice and graphics-plotting expectations, you can find a good explanation here:
This not just something that MATLAB has to contend with, see the indexing option here:
and another blog, apparently written by someone with a graphics background:
Image Analyst
on 30 Dec 2021
Edited: Image Analyst
on 30 Dec 2021
Craig, if the axis direction defaults don't do it the way you'd like, you can flip the direction of the y axis with
axis ij
axis xy
Use whichever does it the way you want.
Craig
on 30 Dec 2021
Accepted Answer
More Answers (2)
I think you're seeing the difference between meshgrid and ndgrid. Meshgrid is used for plotting, ndgrid for matrix/tensor work
[rr,cc] = ndgrid(1:3,1:4)
[xx,yy] = meshgrid(1:3,1:4)
When I was heavily involved in 3d image processing in grad school I tried to be very very consistent and always use row/col as: the convention, notation, and variable names.
2 Comments
Image Analyst
on 30 Dec 2021
Oh, so that's the difference. I never knew. I just always used meshgrid() for everything since I knew that one and didn't want to learn another function. But I can see how ndgrid() could be useful in some situations.
Craig
on 30 Dec 2021
Craig, you do know that matrices in MATLAB are indexed (row, column), right? Apparently not since if I
choose better names for your loop iterators we get this:
x = 0:pi/10:pi
y = 0:pi/10:2*pi
for xIndex = 1:11
for yIndex = 1:21
z(xIndex,yIndex) = sin(x(xIndex))*sin(y(yIndex))
end
end
Why are you indexing z like that? Well since row is y, M(row, column) is M(y, x). Matrices are not indexed like M(x, y). You should have z(yIndex, xIndex). The obvious solution is to just label the axes:
xlabel('X', 'FontSize', 25);
ylabel('y', 'FontSize', 25);
zlabel('Z', 'FontSize', 25);
axis equal
But make sure you do it right. You can also use meshgrid(), which you should. I don't have surfl() so I used surf():
x = 0:pi/10:pi;
y = 0:pi/10:2*pi;
for xIndex = 1: length(x)
for yIndex = 1: length(y)
z(yIndex,xIndex) = sin(x(xIndex))*sin(y(yIndex));
end
end
[X, Y] = meshgrid(x, y);
surf(X, Y, z);
% Label the first argument to surf(), which is the columns or x values.
xlabel('X', 'FontSize', 25);
% Label the second argument to surf(), which is the rows or y values.
ylabel('Y', 'FontSize', 25);
zlabel('Z', 'FontSize', 25);
axis equal
g = gcf;
g.WindowState = 'maximized';
1 Comment
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!