flipped indexes in 3D array
10 views (last 30 days)
Show older comments
VALENTINA CARMONA MONGUA
on 20 Dec 2021
Answered: Image Analyst
on 20 Dec 2021
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 Comments
Accepted Answer
Image Analyst
on 20 Dec 2021
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 Comments
More Answers (1)
Voss
on 20 Dec 2021
Edited: Voss
on 20 Dec 2021
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 Comments
See Also
Categories
Find more on Line 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!