Sequence that MATLAB plots a matrix. How MATLAB work for that?

2 views (last 30 days)
Hi, want to ask a basic question about MATLAB 3D plotting. I want to know how matlab extract values from a matrix and mapping them to the graph.
When creating a matrix, the matrix is shaped in sequence of (row,col), e.g. A(row, col).
But I found that when plotting, MATLAB treats column as the x-axis, and row as y-axis. So that when plotting, I should say "surf(col,row,result)". But normally, people will think of plotting the matrix by "surf(row,col,result)", because the row is usually the first element and then column.
"surf(col,row,result)" and "surf(row,col,result)" are different graphs, because with specific x-value and y-value the z-values are different. I think "surf(row,col,result)" gives the wrong result, but it actually the normall sequence that is intuitive.
I want to know why is that.
To make it easier to understand, let's start with a simple example.
a = 0:1:10;
b = 100:50:600;
for i=1:length(a)
for j=1:length(b)
result(i,j)=10*a(i)+b(j)^2;
end
end
So for now, "result(i,j)" is a 11x11 matrix. In the matrix, the row is calculated based on elements of "a" (first row is a[1] and second row is a[2]) and column is calculated based on elements of "b" (first column is b[1] and second column is b[2]).
But when plotting, the sequence is reversed. If I plot the 3D graph of a, b and result, I CANNOT say
surf(a,b,result)
because MATLAB plots the column elements on x-axis, and row elements on y-axis.
I need to flip a and b when plotting, by
surf(b,a,result)
Then, row elements (based on "a") are on x-axis, and column elements are on y-axis. This is the correct plotting.
It is not intuitive because the "result" matrix is written in the sequence of "a" and then "b". But when plotting, the sequence should be "b" and then "a".
I just realized this kind of "rule". I don't know what I found is correct or not. Please give some comments. Thanks!
  8 Comments
Jianming Wang
Jianming Wang on 9 Feb 2021
@Adam DanzThanks for listing! I didn't realize this logic (sequence) issue in MATLAB before. I previously used the same range of "a" and "b" so I couldn't distinguish them on the x-y-axis and all my plotting codes are written as "surf(a,b,result)". I am glad that I noticed this issue occassionally, now I have to revise my previous work.
Adam Danz
Adam Danz on 9 Feb 2021
The functions I mentioned use the columns of Z as x-values and rows of Z as y-values when Z is the only input.
Some of those functions behave differently from surf when X and Y are also specified. For example, in image and imagesc, just the bounds of X and Y are referenced (example below). Always refer to the documentation to understand the inputs and always spot-check the results as I'm sure you've done with surf to determine your mistake
d = rand(3,3);
imagesc(100:300, 2:4, d)
set(gca,'YDir','Normal')

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!