Count the number of occurances of an element using accumarray
Show older comments
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.
Accepted Answer
More Answers (1)
Sean de Wolski
on 27 Dec 2012
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)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!