Matrix visualisation with coloured box and numbers

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

Answers (2)

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')
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 Comments

Brilliant thank you!
One slight hiccup with my code is that it now my matrix is inverted -any quick way to solve this? Having a mental block this morning.....
Thanks
I don't know what's inverted but here's a way to reverse it all, change the code so it fits your needs
clf
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
set(gca,'XTick',[],'YTick',[]) %stolen code from Matt example :)
set(gca,'XDir','reverse') %reverse X axis
set(gca,'YDir','reverse') %reverse Y axis
You're probably referring to the y-axis being flipped. So, either
set(gca,'YDir','normal')
or
axis xy
BTW, I don't know if you want the colors to be fixed to the values, or whether you want the full range of colors to be used every time (regardless of value). Paulo's example uses the full color range (by using imagesc); mine uses a fixed color palette. Hence, with Paulo's example, 1 is dark blue, 4 is dark red; however, if you change M to ones(3), the whole image will be green. That is, 1 = dark blue is not absolute, it's relative. In my example, there are Amax fixed colors, so 3 is always the third color in the palette. That's better if you want the colors to symbolize a fixed value, but Paulo's is better if you want flexibility. It depends whether your matrix has a fixed set of values or not.
First I just wanna say thanks for these answers it's been very helpful, thanks everyone.
And can someone explain me these line of code:
set(gca,'XTick',[],'YTick',[],'YDir','normal')
Thanks
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
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.

Sign in to comment.

Categories

Asked:

on 18 Apr 2011

Community Treasure Hunt

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

Start Hunting!