1

I plotted a confusion matrix in Matlab using the code from this link.

However whenever there is a zero on the cell it is still shown. How can I eliminate the printing of 0.00's on the cells?

Sample of my confusion matrix

enter image description here

Community
  • 1
  • 1
kcc__
  • 1,638
  • 4
  • 30
  • 59

2 Answers2

9

After you removed all spaces, find '0.00' and substitute it with spaces again

idx = find(strcmp(textStrings(:), '0.00'));
textStrings(idx) = {'   '};

The complete code will then be:

mat = rand(5);           %# A 5-by-5 matrix of random values from 0 to 1
mat(3,3) = 0;            %# To illustrate
mat(5,2) = 0;            %# To illustrate
imagesc(mat);            %# Create a colored plot of the matrix values
colormap(flipud(gray));  %# Change the colormap to gray (so higher values are
                         %#   black and lower values are white)

textStrings = num2str(mat(:),'%0.2f');  %# Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings));  %# Remove any space padding

%% ## New code: ###
idx = find(strcmp(textStrings(:), '0.00'));
textStrings(idx) = {'   '};
%% ################

[x,y] = meshgrid(1:5);   %# Create x and y coordinates for the strings
hStrings = text(x(:),y(:),textStrings(:),...      %# Plot the strings
                'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim'));  %# Get the middle value of the color range
textColors = repmat(mat(:) > midValue,1,3);  %# Choose white or black for the
                                             %#   text color of the strings so
                                             %#   they can be easily seen over
                                             %#   the background color
set(hStrings,{'Color'},num2cell(textColors,2));  %# Change the text colors

set(gca,'XTick',1:5,...                         %# Change the axes tick marks
        'XTickLabel',{'A','B','C','D','E'},...  %#   and tick labels
        'YTick',1:5,...
        'YTickLabel',{'A','B','C','D','E'},...
        'TickLength',[0 0]);

This gives:

enter image description here

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
1

does this work - do a loop over i and j (spatial dimensions) after textStrings is defined and before it is converted to cell, and set

textStrings(i,j,1:4)='    ';

depending on if mat(i,j) is really close to 0.00 using an if-else statement

Guddu
  • 2,325
  • 2
  • 18
  • 23
  • i haven't tested it yet. that's why my answer started with "does this work" :P let me test it and comment back in 2 min :P – Guddu Jan 19 '14 at 11:30
  • see for yourself `a=rand(3,1); a a=num2str(a,'%0.2f'); a a(2,:)=' '; a a= strtrim(cellstr(a)); a x=[1 2 3]; y=[4 5 6]; plot(x,y,'+'); hold on; text(x(:),y(:),a(:)); xlim([0 4]); ylim([3 7]);` – Guddu Jan 19 '14 at 11:40
  • Robert P. your code works great, i should make myself familiar with `find` and `strcmp` :P – Guddu Jan 19 '14 at 12:15
  • A little side note: The code you posted in the answer won't work. `textStrings(i,j,1:4) = ' ';` implies `textStrings` is a 3D matrix. – Stewie Griffin Jan 19 '14 at 12:30
  • aah. my answer was more of a hint about what to do. but yes you are correct, it won't work- `num2str(mat(:))` converts a matrix to a column vector of strings. my bad :P `i` n `j` would have to be converted to a column dimension – Guddu Jan 19 '14 at 12:47