I would like to be able to return the count of occurences of each element in a vector.
For example if I have a vector:
x=[10 25 4 10 9 4 4]
I expect the result to be
10 2
25 1
4 3
9 1

 채택된 답변

Star Strider
Star Strider 2015년 3월 4일

1 개 추천

Use the hist function:
x=[10 25 4 10 9 4 4];
ux = unique(x);
counts = hist(x, ux);
result = [ux' counts']

댓글 수: 4

Rahul Shelar
Rahul Shelar 2015년 3월 4일
편집: Rahul Shelar 2015년 3월 4일
Thanks its really working using hist function on octave..... I didnt knew about hist function I ve tried like this y=unique(x); disp(y); m=length(y); n=length(x); disp(n);
a=zeros(m);
for i=1:1:m
for j=1:1:n
if(y(i)==x(j))
a(i)=a(i)+1;
end
end
end
a=a(:,1); disp(a);
I really like ur approach.......
Star Strider
Star Strider 2015년 3월 4일
My pleasure!
Image Analyst
Image Analyst 2015년 3월 4일
Also learn about its friends like histc() and imhist(). The histogram is like the PDF (probability distribution function), and related to that is the CDF, cumulative distribution function which you can get from passing the counts into cumsum(). By the way, hist() is now deprecated - they tell you to use histogram() instead.
Star Strider
Star Strider 2015년 3월 4일
Note that Rahul Shelar is using Octave.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2015년 3월 4일

댓글:

2015년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by