how to count the occurrences of a value for an matrix?

조회 수: 3 (최근 30일)
Anusha
Anusha 2014년 3월 4일
답변: Image Analyst 2014년 3월 4일
a= [ 3 4 5; 7 8 9; 3 6 8 ; 4 6 9; 3 6 5]
I expected the ans:
b=
[ 3 3
4 2
5 2
7 1
8 2
9 2
6 3]
all the values must displays the number of occurences

채택된 답변

lvn
lvn 2014년 3월 4일
c=unique(a);
b=[c, sum(bsxfun(@eq,a(:),c'))']
b =
3 3
4 2
5 2
6 3
7 1
8 2
9 2

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 3월 4일
What you're describing is the histogram. You can use the histc() function to get the counts, and then stitch the bins and counts together to get the "b" variable you want.
a = [ 3 4 5; 7 8 9; 3 6 8 ; 4 6 9; 3 6 5]
bins = unique(a);
counts = histc(a(:), bins);
b = [bins, counts]

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by