필터 지우기
필터 지우기

Number of Diagonal ones

조회 수: 3 (최근 30일)
Raviteja
Raviteja 2011년 9월 11일
>> 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 ?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 9월 11일
sum(spdiags(Y,-size(Y,2)+1:1:size(Y,1)-1))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by