MATLAB: Number of Diagonal ones

diagonalslogical?

>> N=10;
>> X=fix(randn(N,N));
>> Y=logical(X)
I have written the above code. Now I want to count the number of ones in a diagonal of Y.
In every NxN matrix there are 2N+1 diagonals. So the result should be the number of ones in each diagonal and have to store in a row vector of size 1x(2N+1).
Is there any command ?

Best Answer

sum(spdiags(Y,-size(Y,2)+1:1:size(Y,1)-1))