如何突出显示或识别矩阵中具有特定值的列?

How to highlight or identify a column with a specific value in matrix?

我想在 MATLAB 中突出显示或标识矩阵中具有特定值的列。假设我有一个矩阵 A = [1 0 1 1 0 1; 1 1 0 0 0 1; 1 0 1 1 0 1; 0 1 1 0 0 1].

上述矩阵的结果是第 5 列,因为它包含所有零。我还想知道是否可以突出显示结果列以供识别。请帮我。我有一个非常大的矩阵来应用这个原则。

合并 find and all to get the column index of the all-zero column like this 怎么样?

A = [1 0 1 1 0 1; 1 1 0 0 0 1; 1 0 1 1 0 1; 0 1 1 0 0 1];
 
ind = find(all(A==0,1))
ind = 
    5

all 的第二个输入参数是指定它沿着第一个维度,即行。在这里它并不是真正必要的,但我发现这是一个很好的做法,因为你总是确定它是正确的尺寸。如果在某些情况下您可能会得到 1xn 向量而不是 mxn,这一点尤其重要。

创建彩色矩阵:

这是一个 hack,我不一定推荐它,但如果你真的想在 MATLAB 中这样做,这是一个替代方案。另外,我认为您在执行此操作时可能会学到很多有关 MATLAB 的知识,因此值得花时间。

您可以使用 imagesc. This will give a plot with only two colors, one for those values that are 1, and one for those that are 0. You can select which colors you want with colormap. Then you create a mesh to determine the location of all the values you want to show, convert the matrix to strings using num2str, and combine it all. You need to experiment some to get the correct locations, as you probably want less padding between the rows than the columns. You can use this answer as a guide. In the end, remove the axes 创建一个包含所有值 1 的彩色图,第 5 列中的值除外 0(或相反,无所谓) .如果您阅读并尝试理解引用答案的每一行,适应起来应该相当简单。

简单的方法:

我有一个非常大的矩阵...”。这样的矩阵通常不是包含在报告中的好主意。但是,如果你真的想要,我实际上建议你从 variable explorer and into MS Excel (or use xlswrite 如果您不止一次这样做)。既然您知道要为哪一列着色,那么单击“颜色按钮”应该相当简单。

用红色突出显示 (stderr)

只是为了证明概念,您 可以 在命令 window 中突出显示您的一些数据,但我不建议实际这样做。考虑以下代码:

A=randi(10,8);
%ind = find(all(A==0,1),1) %for actual data
ind = 5; %manual choice for demonstration

for k=1:size(A,1)
    fprintf('%5d   ',A(k,1:ind-1));
    fprintf(2,'%5d   ',A(k,ind));
    fprintf('%5d   ',A(k,ind+1:end));
    fprintf('\n');
end

首先我们创建一个虚拟矩阵用于演示目的,select 列 ind 突出显示。然后我们在 A 中逐行进行,我们使用 fprintf(...) 以给定的格式写入非突出显示的值,然后使用 fprintf(2,...) 写入 stderr 在红色 中,然后写下该行的其余部分,然后是换行符。请注意,出于某种原因 fprintf(2,...) 不会突出显示最后一个字符,我猜是因为通常这是 \n 并且没有人注意到那里缺少突出显示。

此外,您可以根据自己的需要使用 fprintf 中的格式。如果您需要打印浮点数,'%10.8f' 之类的东西可能会起作用。或者 '%g'。要点是为您的打印设置固定宽度+精度,以获得漂亮的列。

为了完整起见,您可以使处理多个突出显示的列更加混乱:

A=randi(10,8);
%ind = find(all(A==0,1)) %for actual data
ind=[5 2];

fprintf('A = \n\n');
for k1=1:size(A,1)
    for k2=1:size(A,2)
        if ismember(k2,ind)
            fprintf(2,'%5d   ',A(k1,k2));
        else
            fprintf('%5d   ',A(k1,k2));
        end
    end
    fprintf('\n');
end
fprintf('\n');

我还添加了一些额外的打印输出以使其更漂亮。结果:

用蓝色突出显示(link秒)

作为事后的想法,在与 进行了一些讨论之后,我决定在我们这样做的时候做一些过度的事情是值得的。您可以将您的数字变成带有蓝色和下划线的 hyperlinks,利用在 dispfprintf。对应代码如下:

A=randi(10,8);
ind=[5 2];

fieldlen=5; %width of output fields, i.e. 5 in '%5d'

fprintf('A = \n\n');
for k1=1:size(A,1)
    for k2=1:size(A,2)
        if ismember(k2,ind)
            fprintf([repmat(' ',1,fieldlen-length(num2str(A(k1,k2)))) '<a href="matlab:">%d</a>   '],A(k1,k2));
        else
            fprintf('%5d   ',A(k1,k2));
        end
    end
    fprintf('\n');
end
fprintf('\n');

这会将突出显示的列的元素转换为 '<a href="matlab:">3</a>' 形式的字符串,例如值 3。

这里的另一个技巧是以 matlab: 开头的 hyperlinks 被解析为正确的 matlab 命令,当您单击 link 时它们被激活。您可以通过在命令 window 中键入 disp('<a href="matlab:help help">link</a>') 来尝试。通过设置 <a href="matlab:">...</a>,我们确保当有人点击现在 link 值突出显示的数字时不会发生任何事情。

技术说明:我们只想在 link 中包含实际数字(而不是前面的空格),因此我们必须手动检查我们即将要处理的字符串的长度打印(使用 length(num2str(A(k1,k2))))并手动包含数字前的其余空格。这是通过我在开始时设置的参数 fieldlen 完成的:这指定了每个打印字段的总宽度,即如果我们最初有 fprintf('%5d',...) 那么我们需要为 fieldlen=5; 设置同样的效果。结果:

下面显示命令 window 中的矩阵 以及 粗体字 中的匹配列。可能有多个匹配列,可以匹配任意列值

A = [1 0 1 0 0 1; 1 1 0 1 0 1; 1 0 1 0 0 1; 0 1 1 1 0 1]; %// matrix
c = [0;1;0;1]; %// column to be matched
nn = find(all(bsxfun(@eq, A, c),1)); %// indices of matching columns
s = cellstr(num2str(A)); %// cell array of strings, one for each row; all same length
for n = nn %// for each matching column, with index n
    s = regexprep(s, '\S+', '<strong>[=10=]</strong>', n); %// make bold n-th value of each cell
end
s = vertcat(s{:}); %// convert back into a char array; all strings have the same length
disp(s); %// display

本例中的结果是