1

I am working in matlab and I have a matrix which I would like to visualize it by giving a green-ish color to the lowest values per column/row and red-ish to the rest or different levels of red-ish depending on how close or far this values is from the lowest one and lastly print the values within. For example lets say that I have the following matrix:

0.0085 0.0244 0.0335 0.0312 0.0392 0.0392
0.0246 0.0078 0.0234 0.0281 0.0395 0.0395
0.0299 0.0295 0.0108 0.0224 0.0598 0.0598
0.0253 0.0317 0.0236 0.0123 0.0583 0.0583
0.0363 0.0337 0.0500 0.0497 0.0038 0.0583

what I want to achieve is something like this:

enter image description here

is there anyhow a way to achieve the above result?

I was checking on pcolor or imagesc functions but I couldn't get along with something. I found these links here and here that they kind of try to do something similar but I couldn't get it to change it to my needs. Therefore, does anybody know if it is possible something like that to be done and how?

Thanks.


Update:

In case that I want to add also some titlebars on the top and on the side, look below:

enter image description here

Community
  • 1
  • 1
ttsesm
  • 917
  • 5
  • 14
  • 28
  • It looks like you try to force MATLAB to be EXCEL... – EBH Sep 16 '16 at 10:34
  • 1
    Just wanted to save the time from transfering this from matlab to excel. Since I need only the visualization part and it can be done straight forward in matlab why spend time one this. I think your answer will help other people as well. Well done, thanks again ;-). – ttsesm Sep 16 '16 at 12:45

3 Answers3

2

You can find the minimum on each column, and create a matrix that associates a colour with the corresponding index of the minimum.

This example creates a color for the minima of each column.

[~, idx] = min(A);
M = zeros(size(A));
for iCol = 1:size(A,2)
    M(idx(iCol), iCol) = 1;
end
imagesc(M);

Similarly, you can create a function to assign the desired colour to each value. If you each to print the string of the values on, you can use the text function.

serigado
  • 168
  • 1
  • 7
  • thanks @serigado your answer is on the same path as BMH's one ;-) – ttsesm Sep 16 '16 at 10:20
  • actually BMH's answer was taken from mine, implementing the colorbar and adding strings with the text function as I had said. – serigado Sep 16 '16 at 10:34
  • To be honest, I didn't see your answer when I wrote mine. Only after I posted it I saw you suggested something in the same direction. I decided to leave it because it give the full solution, which other parts of are also important (like how to use `text` correctly). Anyway, you got my upvote too ;) – EBH Sep 17 '16 at 18:12
2

Here is a quick option:

A = [0.0085 0.0244 0.0335 0.0312 0.0392 0.0392
    0.0246 0.0078 0.0234 0.0281 0.0395 0.0395
    0.0299 0.0295 0.0108 0.0224 0.0598 0.0598
    0.0253 0.0317 0.0236 0.0123 0.0583 0.0583
    0.0363 0.0337 0.0500 0.0497 0.0038 0.0583];
back = [1 0 0];
headers = [0.5 0.5 0.5];
minima = [0 1 0];
map = [back ; headers; minima];
colormap(map)
[~,ind] = min(A);
B = zeros(size(A));
for k = 1:size(A,2)
    B(ind(k),k) = 1;
end
B = [ones(1,size(B,2))*0.5;B];
B = [ones(size(B,1),1)*0.5 B];
imagesc(B)
axis off
[y,x]=ndgrid((1:size(A,1)),(1:size(A,2)));
row_titles = num2str((1:size(A,1)).'); % could be any vector...
text(ones(size(A,1),1),2:size(A,1)+1,row_titles,'FontSize',16,'HorizontalAlignment','center',...
    'VerticalAlignment','middle','Color','w')
coloumn_titles = num2str((1:size(A,2)).'); % could be any vector...
text(2:size(A,2)+1,ones(size(A,2),1),coloumn_titles,'FontSize',16,'HorizontalAlignment','center',...
    'VerticalAlignment','middle','Color','w')
text(x(:)+1,y(:)+1,num2str(A(:)),'FontSize',16,'HorizontalAlignment','center',...
    'VerticalAlignment','middle')

which gives:

minfig

and you can change the headers, back and minima colors to fit your prefered style.

EBH
  • 10,350
  • 3
  • 34
  • 59
  • thanks for the answer it seems quite more efficient in regards to Rotem's answer for that reason you got the correct answer. However, what if I want also to add some titlebars, check my update in my initial post. – ttsesm Sep 16 '16 at 10:09
  • 1
    also I figured out that the for loop can be skipped with the following alternative code: rows = 1:size(A,2); linInd=sub2ind(size(A),ind,rows); B(linInd) = 1; and some transparency can be added with: him = imagesc(B); set(him,'AlphaData',0.5); – ttsesm Sep 19 '16 at 13:53
  • 1
    Correct, I just tried to keep the answer simple, and focus on the basic visualizition method. – EBH Sep 19 '16 at 14:08
1

You can create it using insertText.

Use something like the following sample:

A = [0.0085 0.0244 0.0335 0.0312 0.0392 0.0392
     0.0246 0.0078 0.0234 0.0281 0.0395 0.0395
     0.0299 0.0295 0.0108 0.0224 0.0598 0.0598
     0.0253 0.0317 0.0236 0.0123 0.0583 0.0583
     0.0363 0.0337 0.0500 0.0497 0.0038 0.0583];

I = zeros(92, 348, 3, 'uint8');
I(:,:,1) = 255;I(:,:,2) = 199;I(:,:,3) = 206; %Background color

text_str = cell(length(A(:)), 1);
box_color = zeros(length(A(:)), 3);
text_color = zeros(length(A(:)), 3);
position = zeros(length(A(:)), 2);
for y = 0:size(A, 1)-1
    for x = 0:size(A,2)-1
        index = y*size(A,2) + x + 1;
        val = A(y+1, x+1);
        text_str{index} = [' ', num2str(val, '%0.4f'), ' '];
        position(index, 1) = x*58 + 1;
        position(index, 2) = y*18 + 1;
        if (val == min(A(:, x+1)))
            %Green
            box_color(index, :) = uint8([198, 239, 206]);
            text_color(index, :) = uint8([0, 100, 0]);
        else
            %Red
            box_color(index, :) = uint8([255, 199, 206]);
            text_color(index, :) = uint8([100, 0, 0]);
        end
    end
end

I = insertText(I, position, text_str, 'FontSize', 12, 'BoxColor', box_color, 'TextColor', text_color, 'BoxOpacity', 1);

figure;imshow(I);

Note: In newer version of Matlab (newer than I used), you can also select the font.

enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • thank you for the answer. Though with your solution I think you can apply more customizations in regards to EBH's answer I think EBH's is a bit more simple to understand and efficient in lines of code as well. If I could mark your answer as correct I would do it but unfortunately I can't. You got my upvote though ;-). What about adding some titlebars? – ttsesm Sep 16 '16 at 10:20
  • EBH's answer is absolutlly better, he also got an upvote from me. – Rotem Sep 16 '16 at 12:14