Arrays 检查方阵中的所有诊断是否正确

Arrays 检查方阵中的所有诊断是否正确,arrays,matlab,matrix,Arrays,Matlab,Matrix,我试图检查在一个方阵中,在所有可能的对角线和反对角线中是否有多个真值,并返回true,否则返回false。 到目前为止,我尝试了以下方法,但没有涵盖所有可能的对角线: 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; els

我试图检查在一个方阵中,在所有可能的对角线和反对角线中是否有多个真值,并返回true,否则返回false。 到目前为止,我尝试了以下方法,但没有涵盖所有可能的对角线:

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的第一步,有没有一个特定的函数或更好的方法来实现我想要的结果?

一种方法:

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
样本运行:

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
输入:

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
输出:

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

注意:此方法仅考虑主诊断和主反诊断(考虑您提供的示例)。如果您想使用@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))])

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

[ii, jj] = find(A);
res = (numel(unique(ii-jj)) < numel(ii)) || (numel(unique(ii+jj)) < numel(ii));
[ii,jj]=find(A);
res=(numel(unique(ii+jj))
是否可以编辑以显示矩阵
A
?@SanthanSalai使用输出样本更新此仅检查主对角线和反对角线,对吗?@LuisMendo是。从这个例子中,我认为OP只想要这个。也许我的解释是错误的,这绝对是一种让你的答案更吸引人的方式。因为完全正确的答案来自路易斯,所以我不得不把接受权转移给他。不过,非常感谢+@FeliceM没关系:)路易斯的回答是完整的、聪明的&一切。。它值得接受:)