【问题标题】:Check all diags in square matrix for true检查方阵中的所有诊断是否为真
【发布时间】:2015-08-08 22:59:20
【问题描述】:

我正在尝试检查方阵中所有可能的对角线和反对角线中是否有多个真值,并返回真,否则返回假。 到目前为止,我已经尝试过以下方法,但并未涵盖所有可能的对角线:

n=8; %matrix dimension 8 x 8
diag= sum(A(1:n+1:end));
d1=diag>=2;
antiDiag=sum(A(n:n-1:end));
d2=antiDiag>=2;

if ~any(d1(:)) || ~any(d2(:))
    res= true;

else
    res=false;
end

这是假的:

 0     0     0     0     0     1     0     0
 0     0     0     0     0     0     0     0
 1     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0
 0     0     0     1     0     0     0     0
 0     1     0     0     0     0     0     0
 0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     1

这是真的:

 0     0     0     0     0     0     0     1
 0     0     0     0     0     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

由于这是我使用 Matlab 的第一步,是否有特定的功能或更好的方法来实现我正在寻找的结果?

【问题讨论】:

  • 你能编辑显示矩阵A吗?
  • @SanthanSalai 更新输出样本

标签: arrays matlab matrix


【解决方案1】:

要检测 any 对角线或对角线(不仅仅是主对角线和对角线)中是否有多个非零值:获取非零值的行和列索引,iijj;然后检查ii-jj(对角线)或ii+jj(对角线)的任何值是否重复:

[ii, jj] = find(A);
res = (numel(unique(ii-jj)) < numel(ii)) || (numel(unique(ii+jj)) < numel(ii));

【讨论】:

    【解决方案2】:

    一种方法:

    n=8;                         %// size of square matrix
    A = logical(randi(2,n)-1);   %// Create a logical matrix of 0s and 1s
    
    d1 = sum(A(1:n+1:end));      %// sum all the values of Main diagonal
    d2 = sum(A(n:n-1:end-1));    %// sum all the values of Main anti-diag
    result = d1>=2 | d2>=2       %// result is true when any one of them is > than or = to 2
    

    示例运行:

    输入:

    >> A
    
    A =
    
     0     1     1     1     1     0     1     0
     0     1     1     1     1     1     0     0
     0     1     0     1     1     0     0     1
     0     1     1     0     1     1     0     0
     0     1     0     1     1     0     0     1
     1     0     0     0     1     1     0     1
     1     1     1     1     1     1     0     0
     1     1     1     1     0     0     0     1
    

    输出:

    result =
    
     1
    

    注意:此方法仅考虑 Main diag 和 Main Anti-Diag(考虑您提供的示例)。如果你想要所有可能的诊断,the other answer from Luis Mendo 是要走的路

    【讨论】:

    • 这只会检查 main 对角线和反对角线,对吧?
    • @LuisMendo 是的。从这个例子中,我认为 OP 只想要那个。可能是我错了
    • 对于那个解释,这绝对是要走的路
    • 非常感谢您的回答。由于完整的正确答案来自路易斯,我不得不将接受转给他。不过,非常感谢+
    • @FeliceM 没关系 :) 路易斯的回答很完整,很聪明,一切都......值得接受 :)
    【解决方案3】:

    使用@Santhan Salai的生成技术,我们可以使用diag函数(拉出矩阵的主对角线),fliplr翻转中心列,any减少到单个值.

    n=8;                         %// size of square matrix
    A = logical(randi(2,n)-1);   %// Create a logical matrix of 0s and 1s
    
    any([diag(A) ; diag(fliplr(A))])
    

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 2016-02-16
      • 2021-03-12
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多