Matrix visualisation with coloured box and numbers

조회 수: 7 (최근 30일)
Jennifer
Jennifer 2011년 4월 18일
Hello,
I wonder if someone can please help me. I am trying to visualizes a NxM matrix by plotting each matrix entry as a colored square (box). The entry value itself should also be displayed. (Similar to a sudoku grid but with each number value being depicted by a colour as well).
Matrix = [1,2,3;4,3,2;1;4;2]
Matrix =
1 2 3
4 3 2
1 4 2
Thus I would like to plot this matrix as shown above.
Any help much appriciated. Thanks

답변 (2개)

Matt Tearle
Matt Tearle 2011년 4월 18일
imagesc and text. If it's like sudoku in that the range of values is fixed, use image and a custom colormap.
EDIT TO ADD: I realized that it's a bit more involved than I made it sound, so here's an example that makes it all pretty:
n = 4;
m = 5;
Amax = 7;
cmap = autumn(Amax);
A = randi(Amax,m,n);
figure
image(A)
colormap(cmap)
set(gca,'XTick',[],'YTick',[],'YDir','normal')
[x,y] = meshgrid(1:n,1:m);
text(x(:),y(:),num2str(A(:)),'HorizontalAlignment','center')

Paulo Silva
Paulo Silva 2011년 4월 18일
Following Matts answer here's one simple example
M=[1,2,3;4,3,2;1,4,2]
imagesc(M)
for a=1:3
for b=1:3
text(b,a,num2str(M(a,b)))
end
end
  댓글 수: 7
Paulo Silva
Paulo Silva 2011년 4월 23일
XTicks and YTicks are those numbers that appear usually below and on the left side of the axes, that code removes them, the YDir is the direction of the increment, close to the origin is the lowest value with the normal option, the reverse option makes the biggest value close to the origin of the axes.
You can have a better explanation at http://www.mathworks.com/help/techdoc/ref/axes_props.html
Matt Tearle
Matt Tearle 2011년 4월 23일
To deconstruct the syntax: gca is a function that returns a handle to the current axes. So that line of code sets three properties of the current axes object (Paulo explained what they are very nicely) The x/ytick properties should be set to an array of values; [] is an empty array. Hence, you get no tick marks on the axes.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by