如何避免在混淆矩阵中显示零值

时间:2014-01-19 10:03:29

标签: matlab matlab-figure

我使用this link中的代码在Matlab中绘制了一个混淆矩阵。

然而,只要细胞上有零,它仍会显示。如何消除单元格上0.00的打印?

我的混淆矩阵样本

enter image description here

2 个答案:

答案 0 :(得分:9)

删除所有空格后,找到'0.00'并再次用空格替换

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

完整的代码将是:

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]);

这给出了:

enter image description here

答案 1 :(得分:1)

这是否有效 - 在定义i之后,在转换为单元格之前,对jtextStrings(空间维度)进行循环,然后设置

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

取决于使用mat(i,j)语句0.00是否真的接近if-else