MATLAB Answers

1

How can I count the occurrences of each element in a vector in MATLAB?

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

 y=[2 1 3 2 1 3 3].

7 Answers

Answer by MathWorks Support Team on 7 Aug 2017
 Accepted Answer

While there is no single function in MATLAB to count occurrences of each element, there are a few ways to count elements in a vector:

1. Logical Indexing:

The following code snippet will give the desired output:

y = zeros(size(x));
for i = 1:length(x)
y(i) = sum(x==x(i));
end

For MATLAB R2016b and later, you can use implicit expansion to further simplify the code:

y = sum(x==x')

2. Binning:

You can use the "hist" and "unique" functions as shown here to do the same:

x = [10 25 4 10 9 4 4]
[a,b]=hist(x,unique(x))

3. Third-Party Tools:

For another workaround, see the following file, 'CountMember.m', that was contributed by a MATLAB user to do the same from a single function:

<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7738&objectType=file>

Note that MathWorks does not guarantee or warrant the use or content of submissions to the MATLAB Central File Exchange. Any questions, issues, or complaints should be directed to the contributing author.

  4 Comments

Show 1 older comment

the above comment is Exactly true.. as I faced the exam same problem while looking for a solution..

how to reduce 2 3 3 4 4 4 2 matrix to 2 3 4 2

Solution 2 does not produce the desired output. The answer by Andrei Bobov below does.

Log in to comment.


Answer by Andrei Bobrov
on 14 Aug 2014

[a,b] = histc(x,unique(x));
y = a(b);

  3 Comments

I'm working with a small variant of the original problem, where I want it to count the number of occurrences of each whole number till 13.

So if my input is

x = [1,1,1,2,4,5,5]

I need an output

y = [3,1,0,1,2]

How do I do this?

Log in to comment.


Answer by Julian Hapke on 1 Jun 2017
Edited by Julian Hapke on 1 Jun 2017

here is another one:

sum(bsxfun(@eq,x,x'),1)

or if you want the output to be the same orientation as input

sum(bsxfun(@eq,x,x'),(size(x,2)==1)+1)

  1 Comment

the second solution exhibits pathological tendencies...

Log in to comment.


Answer by Josh
on 14 Aug 2014

This is kind of awkward since it requires using the input array within the anonymous function, but:

y = arrayfun(@(t)nnz(x==t), x);

should do the trick, too.

  0 Comments

Log in to comment.


Answer by mittal54
on 16 May 2015
Edited by Walter Roberson
on 16 Aug 2017

numbers=unique(v);       %list of elements
count=hist(v,numbers);   %provides a count of each element's occurrence

this will give counts. and if you want to have a nice graphical representation then try this

bar(accumarray(v', 1))

  1 Comment

When using hist() pay attention to Dan's comment above pointing out a flaw in the approach. This flaw is not shared by Andrei's histc approach above.

Log in to comment.


Answer by lamghari
on 30 Nov 2015

Hi, I want to count the number of followed occurrences of each element in a vector.

So if my input is

x = [1 1 1 2 2 1 1 2 5 5]

I need an output

y = [1 2 1 2 5;3 2 2 1 2] How do I do this?


Answer by Truong Phan on 16 Aug 2017

I need help to count the occurrences of each element in a matrix. Thanks

  1 Comment

[uvals, ~, uidx] = unique(YourArray);
output = [uvals, accumarray(uidx, 1)];

This would produce an N x 2 array with the first column being the unique array values and the second column being the associated count.

Log in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today