Asked by MathWorks Support Team
on 11 Jan 2012

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].

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:

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.

Answer by Andrei Bobrov
on 14 Aug 2014

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

y = a(b);

Harshavardhan Thyagarajan
on 3 Aug 2015

Thanks Andrei, that works!

SANA
on 27 Apr 2018

Tech Support
on 2 May 2018

Hi,

If you are still experiencing this issue, please contact MathWorks support:

-Justin

Sign 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)

Johannes Korsawe
on 1 Jun 2017

the second solution exhibits pathological tendencies...

Sign in to comment.

Answer by Razvan Carbunescu
on 9 May 2019

>> x=[10 25 4 10 9 4 4]';

>> grouptransform(x,x,@numel)

ans =

2

1

3

2

1

3

3

>>[GC,GR]=groupcounts(x)

GC =

3

1

2

1

GR =

4

9

10

25

Razvan Carbunescu
on 6 Jun 2019

Masoud Mirzaei
on 6 Jun 2019

I meant "the same number" in column 1. Here is the expected solution:

(22 , 33) 2

(22 , 44) 2

(33 , 44) 1

and the other pairs have the value of 0.

THe actual problem consists of > 1 million elements. So I am looking for a smaort solution rather than using some loops.

Razvan Carbunescu
on 7 Jun 2019

Sign in to comment.

Answer by Truong Phan
on 16 Aug 2017

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

Walter Roberson
on 16 Aug 2017

[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.

Sign 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))

Walter Roberson
on 16 May 2015

Sign 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?

Andrei Bobrov
on 30 Nov 2015

y = [x(t); diff([find(t),numel(x)+1])]

Jan
on 29 Jan 2017

[B, N] = RunLength(X);

Y = [B; N]

Sign in to comment.

Answer by Ramon Villamangca
on 25 Oct 2018

Edited by Ramon Villamangca
on 25 Oct 2018

This is how I did it:

numOccur = sum(arrayfun(@(x) x == elem,vec))

where 'elem', is the element to search in the given vector 'vec'.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Dan (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/96504-how-can-i-count-the-occurrences-of-each-element-in-a-vector-in-matlab#comment_219143

## Riyasat (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/96504-how-can-i-count-the-occurrences-of-each-element-in-a-vector-in-matlab#comment_251408

## shashika iresh (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/96504-how-can-i-count-the-occurrences-of-each-element-in-a-vector-in-matlab#comment_395721

## Paolo Binetti (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/96504-how-can-i-count-the-occurrences-of-each-element-in-a-vector-in-matlab#comment_424191

## Ujjwal Mohanty (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/96504-how-can-i-count-the-occurrences-of-each-element-in-a-vector-in-matlab#comment_539294

Sign in to comment.