Filter löschen
Filter löschen

count the number of ones in each row

4 Ansichten (letzte 30 Tage)
kurdistan mohsin
kurdistan mohsin am 6 Jun. 2022
Bearbeitet: KSSV am 6 Jun. 2022
hi, i have the below N by N matrix and i want to count the number of ones in each row , anyone can help ?
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 1 1 1
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 1 1
0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0

Antworten (1)

KSSV
KSSV am 6 Jun. 2022
Bearbeitet: KSSV am 6 Jun. 2022
A = [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 1 1 1
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 1 1
0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0] ;
[m,n] = size(A) ;
C = zeros(m,1) ;
for i = 1:m
C(i) = nnz(A(i,:)) ;
end
C
C = 10×1
0 0 3 1 1 1 1 3 2 2
Or Simply:
C = sum(A==1,2)
C = 10×1
0 0 3 1 1 1 1 3 2 2

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by