surf indices reversed?

2 ビュー (過去 30 日間)
Craig 2021 年 12 月 29 日
編集済み: Stephen23 2021 年 12 月 30 日
Quick question: It seems surf and surfl have indices reversed. Is this correct? For example, if I type
y = 0:pi/10:2*pi
for i = 1:11
for j = 1:21
z(i,j) = sin(x(i))*sin(y(j))
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 件のコメント
Craig 2021 年 12 月 30 日



Matt J
Matt J 2021 年 12 月 29 日
編集済み: Matt J 2021 年 12 月 29 日
Yes, the x-axis traverses the rows of z and the y-axis traverses the columns.
For some reason, many people prefer their x-axis to be horizontal.
  3 件のコメント
Craig 2021 年 12 月 29 日
Thanks, Matt. And thanks for your first answer, too. My first comment was a little short.
Yes, when I read "traverse the rows" I parsed that as moving from row to another ("traverse a row" I would have read as going down one row, from one column to the next. Don't know if that is right, but what you said makes sense now).
The rotation is not a language, just mathematical conventions. When you access the i-jth entry of a matrix z ( z(i,j) in Matlab), you go down to the ith row and across to the jth column. In a function z(x,y), for z(x_i, y_j) you would go across the x-axis to the ith entry on our grid, then up to the jth entry on the grid. Anyway, the different conventions for plotting and matrix entries cause some confusion, but I understand what Matlab is doing now. Not intuitive to me, but I can use it.


その他の回答 (2 件)

Sean de Wolski
Sean de Wolski 2021 年 12 月 30 日
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)
rr = 3×4
1 1 1 1 2 2 2 2 3 3 3 3
cc = 3×4
1 2 3 4 1 2 3 4 1 2 3 4
[xx,yy] = meshgrid(1:3,1:4)
xx = 4×3
1 2 3 1 2 3 1 2 3 1 2 3
yy = 4×3
1 1 1 2 2 2 3 3 3 4 4 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 件のコメント
Craig 2021 年 12 月 30 日
Thanks, Sean!


Image Analyst
Image Analyst 2021 年 12 月 29 日
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))
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));
[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 件のコメント
Craig 2021 年 12 月 29 日
Thanks, image analyst. I do know that matrices are labeled row, column. The "apparently not" comment sounds to me condescending, but it's easy to misinterpret things online so I won't worry about it.
Read the last response to Matt above. The issue is with the different conventions for matrices compared to plotting. If I give you a point (4,2,3), you x=4, y=2, and z =3 (across 4 units, then up, then out of the page in my mind, or across, back, up). But in matrix z, z(4,2)=3 you go down to the 4th row, the across to the seocnd column, and input 3.





Community Treasure Hunt

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

Start Hunting!

Translated by