Count the number of times a value occurs in a specific of an array
조회 수: 416 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (2개)
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
2014년 7월 17일
Try the following to find the number of times '2' occurs in the second row
length(find(A(2,:)==2))
댓글 수: 1
Matz Johansson Bergström
2014년 7월 17일
or the shorter and more to the point
sum( A(2,:)==2 )
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!