Representing a Matrix Graphically (but not exactly)

Hello All, I'm new here. I tried looking for this topic, but couldn't find anything.
What I want to do is to represent a matrix graphically. That means that I want the values of each cell in the matrix to appear on a graph on the X,Y coordinates of the cell. For instance, if I have the next matrix:
5 2 1
0 2 3
9 3 7
So the 5 in the first row will be at (1,1), 2 at (1,2), 1 at (1,3), etc. The values can be replaced by '*' or any other symbol really.
Thanks, eliya

 採用された回答

Walter Roberson
Walter Roberson 2011 年 3 月 3 日

0 投票

Or if you want a representation that is color coded but contains no symbols
imagesc(ArrayName)

その他の回答 (4 件)

Paulo Silva
Paulo Silva 2011 年 3 月 3 日

0 投票

a=[5 2 1
0 2 3
9 3 7]
axis([0 size(a,2) 0 size(a,1)])
for b=1:numel(a)
[x,y]=ind2sub(size(a),b)
text(x,y,num2str(a(b)))
end
Matt Fig
Matt Fig 2011 年 3 月 3 日

0 投票

Like this (a surface?):
Z = [5 2 1
0 2 3
9 3 7];
surf(Z.')
xlabel('X')
ylabel('Y')
Matt Tearle
Matt Tearle 2011 年 3 月 3 日

0 投票

z = [5 2 1 <etc>
[m,n] = size(z);
[x,y] = meshgrid(1:n,1:m);
plot3(x(:),y(:),z(:),'o')
or
text(x(:),y(:),num2str(z(:)))
Or even just
image(z), colorbar
BTW, typically you'd think of the columns as the x coordinate, so 2 would actually be at x=2, y=1. If that's not what you want, just flip the role of x and y.

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 3 月 3 日
Or instead of plot3, use scatter3
scatter3(x(:),y(:),z(:))

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

Eliya
Eliya 2011 年 3 月 3 日

0 投票

Thanks everyone. It all worked, but Walter's suggestion to use imagesc was the best as I'm writing a "Game of Life" kind of program. Thanks again for the prompt replies!

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by