两行之间的比较和变化值

Comparison between two rows and change value

我有一个 20 行 20 列的矩阵, 如果第 5 行中的值 1 该列取 0

matric=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 
     1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 
     0 1 1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1;
     0 1 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1;
     0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;];
if (matric(5,:)==1)
matric(1:5,1:end)=0;end

我试着比较第二行和第五行 如果我们在第 2 行和第 5 行中有“1” 第2行取0

if (matric(5,:)==matric(2,:)==1)
matric(2,1:end)=0;end

你有什么想法吗

谢谢

期望的输出是:

 matric=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 
     1 0 1 1 0 0 1 1 1 1 0 1 0 1 1 1 1 0 1 0 ;2row will change 
     0 1 1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1;
     0 1 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1;
     0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1; % 5 row

您可以使用逻辑索引

强烈建议您阅读以下参考资料:


使用以下代码行:

Put 0 in all those columns that have a value of 1 in row 5:

matric(:, matric(5, :) == 1) = 0;

Put 0 in all those columns of row 2 that have a value of 1 both in rows 2 and 5:

matric(2, matric(2, :) == matric(5, :)) = 0;

您可以使用逻辑索引来实现这一点。现在我必须说,根据你的描述,我对你想要实现的目标感到有点困惑,但根据你的代码,第一条语句可以按如下方式完成:

matric(1:5,matric(5,:)==1) = 0;

第二个看起来像:

matric(2,matric(5,:)==1 & matric(2,:)==1)=0;