I am trying to check if in a square matrix there is more than one true value in all possible diagonals and anti-diagonals, and return true, otherwise false.
So far I have tried as following but is not covering all possible diagonals:

```
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

```

this is a false:

```
 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

```

this is a true:

```
 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

```

Since these are my first steps in using Matlab, is there a specific function or a better way to achieve the result I am looking for?
['https://Stackoverflow.com/questions/30472618', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2280374/']
To detect if there are more than one nonzero value in *any* diagonal or anti-diagonal (not just the main diagonal and antidiagonal): get the row and column indices of nonzero values, `ii` and `jj`; and then check if any value of `ii-jj` (diagonals) or `ii+jj` (anti-diagonals) is repeated:

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

```
**One approach:**

```
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

```

**Sample run:**

**Inputs:**

```
>> 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

```

**Output:**

```
result =

 1

```

**Note:** This approach considers only the Main diag and Main Anti-Diag (considering the example you provided). If you want for all possible diags, [the other answer from Luis Mendo](https://stackoverflow.com/a/30476454/2111163) is the way to go