Count the number of times a value occurs in a specific of an array

430 ビュー (過去 30 日間)
Tyler
Tyler 2014 年 7 月 17 日
コメント済み: KARANAM ANILBABU 2019 年 2 月 10 日
Given a array, is there any way to count the number of times a value occurs within a specific row of that array?
For example, if I have a array:
A=[1,2,5,2,3,4,2; 4,2,1,5,3,2,3; 1,4,2,3,2,2,1];
I want to know how many times the value '2' occurs in the second row
Thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 17 日
編集済み: Azzi Abdelmalek 2014 年 7 月 17 日
A=[1,2,5,2,3,4,2; 4,2,1,5,3,2,3; 1,4,2,3,2,2,1]
sum(A(2,:)==2)
%or
nnz(A(2,:)==2)
  2 件のコメント
Tyler
Tyler 2014 年 7 月 29 日
Thanks Azzi!
If I had the same problem, but I wanted to determine how many values were between a certain range, (ie. between 2 and 4) how would I do that?
Image Analyst
Image Analyst 2014 年 7 月 29 日
Tyler, you can use histc(). See my answer or Star's answer.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 7 月 17 日
In general, you can use histc() to find the counts for all of the numbers in one shot:
A=[1,2,5,2,3,4,2; 4,2,1,5,3,2,3; 1,4,2,3,2,2,1];
edges = unique(A)
counts = histc(A(:), edges)
In the command window:
edges =
1
2
3
4
5
counts =
4
8
4
3
2

Geoff Hayes
Geoff Hayes 2014 年 7 月 17 日
Try the following to find the number of times '2' occurs in the second row
length(find(A(2,:)==2))

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by