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

1.221 visualizzazioni (ultimi 30 giorni)
MathWorks Support Team il 11 Gen 2012
Commentato: Walter Roberson il 26 Apr 2022
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].

### Risposta accettata

MathWorks Support Team il 27 Feb 2020
Modificato: MathWorks Support Team il 27 Feb 2020
As of MATLAB R2019a, you can use the “groupcounts” function to compute the number of times an element appears in a vector as a summary. In other words, the elements of the below output “GC” are the counts of the corresponding element values in “GR” (from the original input vector “x”):
x = [10 25 4 10 9 4 4]';
[GC,GR] = groupcounts(x)
GC =
3
1
2
1
GR =
4
9
10
25
---
As of MATLAB R2018b, you can use the “grouptransform” function if you want to compute the number of times an element appears in a vector and output that count for each corresponding element of the input vector. For example:
x = [10 25 4 10 9 4 4]';
y = grouptransform(x,x,@numel)
y =
2
1
3
2
1
3
3
---
Prior to MATLAB R2018b, while there is no single function 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.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Walter Roberson il 19 Mag 2020
S = 'aaabb'
nnz(triu((S'=='a') & S=='b'))
Walter Roberson il 26 Apr 2022
https://www.mathworks.com/help/matlab/ref/double.groupcounts.html#mw_92fbcf5a-2ab5-45d0-ac09-68c1986c269f
when you use groupcounts then the groups are in the order returned by unique('sorted')

Accedi per commentare.

### Più risposte (3)

Andrei Bobrov il 14 Ago 2014
[a,b] = histc(x,unique(x));
y = a(b);
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Andrei Bobrov il 3 Ago 2015
y = accumarray(x(:),1)

Accedi per commentare.

Razvan Carbunescu il 9 Mag 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
##### 4 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Razvan Carbunescu il 6 Giu 2019
For the example you gave above how does the solution look and what does 'similar number' for the first column mean?
Razvan Carbunescu il 7 Giu 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

Accedi per commentare.

Julian Hapke il 1 Giu 2017
Modificato: Julian Hapke il 1 Giu 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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Johannes Korsawe il 1 Giu 2017
the second solution exhibits pathological tendencies...

Accedi per commentare.

### Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

R14SP1

### Community Treasure Hunt

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

Start Hunting!

Translated by