필터 지우기
필터 지우기

Count the number of occurances of an element using accumarray

조회 수: 5 (최근 30일)
Ms. Mat
Ms. Mat 2012년 12월 27일
Now I am trying to find the occurance of an element in a vector using
sum(dta(:,size(dta,2))==3);
How can accumarray be used to find the frequency of elements ?
A = [7 11 2 3 4 5 4 7 7 2 1 4 1];
How can I get a result such as,
  • 7 3
  • 11 1
  • 2 2
  • 3 1
  • 4 2
and so on.
Thanks in Advance.
P.S: I looked through other threads, but did not understand how it worked. The example given was count = accumarray(A',1) and the result was a vector which was not clear to me.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 27일
편집: Azzi Abdelmalek 2012년 12월 27일
A = [7 11 2 3 4 5 4 7 7 2 1 4 1]
[a,b,c ]=unique(A,'stable')
out=[a' accumarray(c,1)]
  댓글 수: 3
Ms. Mat
Ms. Mat 2013년 1월 12일
what is this option 'stable'. I work with 2009 version. I dont have this option. Also could you please explain wat really the code is doing.
Thank You
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 12일
A = [7 11 2 3 4 5 4 7 7 2 1 4 1]
[a,b,c ]=unique(A) % you can remove 'stable'
% a is the array containing unique value but in sorted order
%a=[ 1 2 3 4 5 7 11], if you remove 'stable', the result will be sorted
%c=[6 7 2 3 4 5 4 6 6 2 1 4 1] gives the indices of each value of A in a
% for example the 2nd value 11 in A is the 7nth in a
out=[a' accumarray(c,1)], %the result is sorted
1 2
2 2
3 1
4 3
5 1
7 3
11 1

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 12월 27일
I would use histc on the output vector c that you have above from unique()
Frankly you should be able to skip all of that altogether:
[uv, idx] = unique(A);
n = histc(A,uv);
nA = n(idx)
(not tested in ML)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by