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

 Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Mzo. de 2011
Or if you want a representation that is color coded but contains no symbols
imagesc(ArrayName)

Más respuestas (4)

Paulo Silva
Paulo Silva el 3 de Mzo. de 2011
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 el 3 de Mzo. de 2011
Like this (a surface?):
Z = [5 2 1
0 2 3
9 3 7];
surf(Z.')
xlabel('X')
ylabel('Y')
Matt Tearle
Matt Tearle el 3 de Mzo. de 2011
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 comentario

Walter Roberson
Walter Roberson el 3 de Mzo. de 2011
Or instead of plot3, use scatter3
scatter3(x(:),y(:),z(:))

Iniciar sesión para comentar.

Eliya
Eliya el 3 de Mzo. de 2011

0 votos

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!

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by