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

MathWorks Support Team

### MathWorks Support Team (view profile)

님이 질문을 제출함. 11 Jan 2012
최근 활동 Razvan Carbunescu

### Razvan Carbunescu (view profile)

님이 댓글을 추가함. 7 Jun 2019
MathWorks Support Team

### MathWorks Support Team (view profile)

님이 답변을 채택함.
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].

R14SP3

## 답변 수: 9

### MathWorks Support Team (view profile)

on 7 Aug 2017

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.

표시 이전 댓글 수: 2
shashika iresh

### shashika iresh (view profile)

on 4 Oct 2016
how to reduce 2 3 3 4 4 4 2 matrix to 2 3 4 2
Paolo Binetti

### Paolo Binetti (view profile)

on 29 Jan 2017
Solution 2 does not produce the desired output. The answer by Andrei Bobov below does.
Ujjwal Mohanty

### Ujjwal Mohanty (view profile)

on 25 Feb 2018
@ Shashika iresh..... z = sort(unique(y)) where y = [2 3 3 4 4 4 2]

로그인 to comment.

### Andrei Bobrov (view profile)

on 14 Aug 2014

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

#### 댓글 수: 5

표시 이전 댓글 수: 2
Harshavardhan Thyagarajan

### Harshavardhan Thyagarajan (view profile)

on 3 Aug 2015
Thanks Andrei, that works!
SANA

### SANA (view profile)

on 27 Apr 2018
I want to count the different number of elements in an array, whether unique or repeating but different, for example I have an array A = [1 2 2 3 1 2 3 3 3 4 1 5 7 9 9 2 8] I want answer to be; ans = 8 How to do this plz help
Tech Support

on 2 May 2018
Hi,
-Justin

로그인 to comment.

### Razvan Carbunescu (view profile)

on 9 May 2019

There is a simpler way of answering this now using groupcounts(R2019a) or grouptransform(R2018b):
>> 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

#### 댓글 수: 7

표시 이전 댓글 수: 4
Razvan Carbunescu

### Razvan Carbunescu (view profile)

on 6 Jun 2019
For the example you gave above how does the solution look and what does 'similar number' for the first column mean?
Masoud Mirzaei

### Masoud Mirzaei (view profile)

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

### Razvan Carbunescu (view profile)

on 7 Jun 2019
This seems like a very different type of problem so unlikely the functions in this topic will help you directly. I'd post this question as a separate thread with the example input/output

로그인 to comment.

### Julian Hapke (view profile)

on 1 Jun 2017
Edited by Julian Hapke

### Julian Hapke (view profile)

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

### Johannes Korsawe (view profile)

on 1 Jun 2017
the second solution exhibits pathological tendencies...

로그인 to comment.

### Josh (view profile)

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.

로그인 to comment.

### Truong Phan (view profile)

on 16 Aug 2017

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

Walter Roberson

### Walter Roberson (view profile)

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.

로그인 to comment.

### mittal54 (view profile)

on 16 May 2015
Edited by Walter Roberson

### Walter Roberson (view profile)

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

### Walter Roberson (view profile)

on 16 May 2015
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.

로그인 to comment.

### lamghari (view profile)

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

### Andrei Bobrov (view profile)

on 30 Nov 2015
y = [x(t); diff([find(t),numel(x)+1])]
Jan

### Jan (view profile)

on 29 Jan 2017
[B, N] = RunLength(X);
Y = [B; N]

로그인 to comment.

### Ramon Villamangca (view profile)

on 25 Oct 2018
Edited by Ramon Villamangca

### Ramon Villamangca (view profile)

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

로그인 to comment.

Translated by