Borrar filtros
Borrar filtros

Number of Diagonal ones

2 visualizaciones (últimos 30 días)
Raviteja
Raviteja el 11 de Sept. de 2011
>> 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 ?

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 11 de Sept. de 2011
sum(spdiags(Y,-size(Y,2)+1:1:size(Y,1)-1))

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by