Why reshaping the matrix gives entirely different plots while using pcolor()

2 ビュー (過去 30 日間)
Mathan
Mathan 2022 年 4 月 6 日
コメント済み: Mathan 2022 年 4 月 7 日
Hello,
I was trying to plot 3 variables with two of them representing the X and Y axes while the third one is used to denote the color, using the pcolor function. I however find that whenever I change the dimensions of the matrix by reshaping them and try plotting again, I get entirely different plots. I was wondering why that is happening - after all its the same datapoints which are being plotted against each other everytime (no matter what the dimension of the matrix is) , how come a mere change in the dimensions of the matrix give different results?
Following is the code which I tried (given for example):
M = randi([1 100],13, 21); % original matrices
N = randi([200 500],13, 21);
P = randi([700 1000],13, 21);
M1 = reshape(M,3,[]); % reshaped matrices from the original matrices
N1 = reshape(N,3,[]);
P1 = reshape(P,3,[]);
M2 = reshape(M,39,[]); % reshaped matrices from the original matrices
N2 = reshape(N,39,[]);
P2 = reshape(P,39,[]);
figure(1);
pcolor(M,N,P);
figure(2);
pcolor(M1, N1, P1);
figure(3);
pcolor(M2, N2, P2);
Thank you.

採用された回答

Walter Roberson
Walter Roberson 2022 年 4 月 6 日
pcolor() is equivalent to surf() followed by view(90) .
surf() does not use the input data directly to compute face color. Instead, surf() uses the input data as indicating the vertices, and does a bilinear interpolation to get the value to use for the face color.
When you reshape() you change the relative order of the nodes, and so change which vertices are combined for a particular face. But because of the interpolation to get the actual face color, that changes the face colors.
  7 件のコメント
Walter Roberson
Walter Roberson 2022 年 4 月 7 日
imagesc('XData', [M(1,1), M(end,end)], 'YData', [N(1,1),N(end,end)], 'CData', P)
Mathan
Mathan 2022 年 4 月 7 日
It still gives out the same result (i.e. different plots whenever reshaping is done). The code is as follows:
M = randi([1 100],13, 21);
N = randi([200 500],13, 21);
P = randi([700 1000],13, 21);
M1 = reshape(M,3,[]);
N1 = reshape(N,3,[]);
P1 = reshape(P,3,[]);
M2 = reshape(M,39,[]);
N2 = reshape(N,39,[]);
P2 = reshape(P,39,[]);
M3 = reshape(M.',1,[]);
N3 = reshape(N,1,[]);
P3 = reshape(P,1,[]);
figure(1);
imagesc('XData', [M(1,1), M(end,end)], 'YData', [N(1,1),N(end,end)], 'CData', P)
figure(2);
imagesc('XData', [M1(1,1), M1(end,end)], 'YData', [N1(1,1),N1(end,end)], 'CData', P1)
figure(3);
imagesc('XData', [M2(1,1), M2(end,end)], 'YData', [N2(1,1),N2(end,end)], 'CData', P2)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by