MATLAB Answers

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

3,940 ビュー (過去 30 日間)
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].

採用された回答

MathWorks Support Team
MathWorks Support Team 2020 年 2 月 27 日
編集済み: MathWorks Support Team 2020 年 2 月 27 日
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
For more information on "groupcounts", please see the documentation link below:
---
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
For more information on "grouptransform", please see the documentation link below:
---
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.

  8 件のコメント

表示 5 件の古いコメント
ABDALHADI ABU ZEYNEH
ABDALHADI ABU ZEYNEH 2020 年 5 月 16 日
how to calculate the occurence of all possibilities in which a comes before b
for example the occurence of a a a b b =6
also abbaab=5
Walter Roberson
Walter Roberson 2020 年 5 月 19 日
Why is abbab 5 ?
If we number the positions 1:5 then you have (1,2), (1,3), (1,5), (4,5) which is 4 combinations.
Walter Roberson
Walter Roberson 2020 年 5 月 19 日
S = 'aaabb'
nnz(triu((S'=='a') & S=='b'))

サインインしてコメントする。

その他の回答 (8 件)

Andrei Bobrov
Andrei Bobrov 2014 年 8 月 14 日
[a,b] = histc(x,unique(x));
y = a(b);

  5 件のコメント

表示 2 件の古いコメント
Harshavardhan Thyagarajan
Harshavardhan Thyagarajan 2015 年 8 月 3 日
Thanks Andrei, that works!
SANA
SANA 2018 年 4 月 27 日
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
Tech Support 2018 年 5 月 2 日
Hi,
If you are still experiencing this issue, please contact MathWorks support:
-Justin

サインインしてコメントする。


Razvan Carbunescu
Razvan Carbunescu 2019 年 5 月 9 日
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 2019 年 6 月 6 日
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 2019 年 6 月 6 日
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 2019 年 6 月 7 日
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

サインインしてコメントする。


Julian Hapke
Julian Hapke 2017 年 6 月 1 日
編集済み: Julian Hapke 2017 年 6 月 1 日
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 件のコメント

Johannes Korsawe
Johannes Korsawe 2017 年 6 月 1 日
the second solution exhibits pathological tendencies...

サインインしてコメントする。


Josh
Josh 2014 年 8 月 14 日
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 件のコメント

サインインしてコメントする。


Truong Phan
Truong Phan 2017 年 8 月 16 日
I need help to count the occurrences of each element in a matrix. Thanks

  1 件のコメント

Walter Roberson
Walter Roberson 2017 年 8 月 16 日
[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.

サインインしてコメントする。


mittal54
mittal54 2015 年 5 月 16 日
編集済み: Walter Roberson 2017 年 8 月 16 日
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 件のコメント

Walter Roberson
Walter Roberson 2015 年 5 月 16 日
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.

サインインしてコメントする。


lamghari
lamghari 2015 年 11 月 30 日
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?

  2 件のコメント

Andrei Bobrov
Andrei Bobrov 2015 年 11 月 30 日
y = [x(t); diff([find(t),numel(x)+1])]

サインインしてコメントする。


Ramon Villamangca
Ramon Villamangca 2018 年 10 月 25 日
編集済み: Ramon Villamangca 2018 年 10 月 25 日
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'.

  0 件のコメント

サインインしてコメントする。

製品


リリース

R14SP3

Community Treasure Hunt

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

Start Hunting!

Translated by