flipped indexes in 3D array
10 次查看(过去 30 天)
显示 更早的评论
I'm defining a constant value for a grid point in a 3D matrix, when I plot it it takes the x value as y and the y value as x:
%define the axis
x=[0:10]
y=[0:10]
a=length(x)
%create 3D array of zeros
T=zeros(a,a,a)
%T(x=4,y=8,k=1)=100 a cosntant value for a grid point with position 4,8 in the slice 1 of T
T(4,8,1)=100
%plotting all values of x ,y and the 1st slice of T
surf(x,y,T(:,:,1))
xlabel('x')
ylabel('y')
%%END OF CODE
the graph is the following:
the value 100 is being given to a point in x=8 and y=4, meaning a flip indexing of T, T(y,x,k) why could this be happen?
Also, the matrix is:
0 个评论
采纳的回答
Image Analyst
2021-12-20
Arrays are indexed at M(row, column, slice), or M(y, x, slice). A common mistake is to put x first in the index list like M(x, y, slice). That is not correct. Row is y, NOT x, so row/y comes first, not x.
Also you might experiment around with calling
axis xy
or
axis ij
after you call a display or plotting function. This will flip the vertical axis so that the origin is at the top or bottom - whichever place you want it at.
0 个评论
更多回答(1 个)
Voss
2021-12-20
编辑:Voss
2021-12-20
Notice the documentation for surf states that input argument X is "x-coordinates, specified as a matrix the same size as Z, or as a vector with length n, where [m,n] = size(Z)" and Y is "y-coordinates, specified as a matrix the same size as Z or as a vector with length m, where [m,n] = size(Z)".
In other words, when using vector inputs as X and Y, the number of elements of X must be the same as the number of columns of Z and the number of elements of Y must be the same as the number of rows of Z. So surf interprets Z as having Y along the first dimension (down) and X along the second dimension (across).
In this case, the number of elements of X and Y are the same, so it's not obvious, but if you try it with say, x = [0:5]; y = [0:10]; surf(x,y,zeros(6,11)); you'll get an error because the third argument must be of size 11-by-6, i.e., you must do surf(x,y,zeros(11,6)); in that case.
All that is to say: transpose your matrix in the call to surf, and it'll work:
surf(x,y,T(:,:,1).')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!