Matrix visualisation with coloured box and numbers

6 次查看(过去 30 天)
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 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by