Counting the number of occurences for specific values in an array

1 次查看(过去 30 天)
I have an array that reads [ 1 2 3 4 5 5 4 3 2 1 ] and I want to count the number of occurences of each element
so that it gives me an array of [ 2 2 2 2 2 ]
I tried using
x(i)=sum[array(i)==array] but it gives me [ 2 2 2 2 2 2 2 2 2 2 2 ]

回答(1 个)

Rik
Rik 2020-5-26
a=[ 1 2 3 4 5 5 4 3 2 1 ] ;
histcounts(a)
If you have non-integer data you can use the unique function to convert your vector to positive integers.

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by