フィルターのクリア

count the number of ones in each row

4 ビュー (過去 30 日間)
kurdistan mohsin
kurdistan mohsin 2022 年 6 月 6 日
編集済み: KSSV 2022 年 6 月 6 日
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

回答 (1 件)

KSSV
KSSV 2022 年 6 月 6 日
編集済み: KSSV 2022 年 6 月 6 日
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

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by