Effacer les filtres
Effacer les filtres

count the number of ones in each row

4 vues (au cours des 30 derniers jours)
kurdistan mohsin
kurdistan mohsin le 6 Juin 2022
Modifié(e) : KSSV le 6 Juin 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

Réponses (1)

KSSV
KSSV le 6 Juin 2022
Modifié(e) : KSSV le 6 Juin 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

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by