【问题标题】:How to avoid displaying zero-values in confusion matrix如何避免在混淆矩阵中显示零值
【发布时间】:2016-05-20 19:27:00
【问题描述】:

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

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

我的混淆矩阵示例

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

    删除所有空格后,找到'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]);
    

    这给出了:

    【讨论】:

      【解决方案2】:

      这行得通吗? 在定义textStrings 之后和转换为单元格之前,对ij(空间维度)进行循环,并设置

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

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

      【讨论】:

      • 我还没有测试过。这就是为什么我的回答以“这行得通”开头的原因:P 让我测试一下并在 2 分钟内回复:P
      • 自己看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]);
      • Robert P. 你的代码很好用,我应该熟悉 findstrcmp :P
      • 附注:您在答案中发布的代码不起作用。 textStrings(i,j,1:4) = ' '; 暗示 textStrings 是一个 3D 矩阵。
      • 啊。我的回答更多的是暗示要做什么。但是是的,您是对的,它不起作用-num2str(mat(:)) 将矩阵转换为字符串的列向量。我的坏 :P i n j 必须转换为列维度
      猜你喜欢
      • 1970-01-01
      • 2019-09-28
      • 2020-03-09
      • 2018-10-31
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多