Counting the number of occurences for specific values in an array

2 views (last 30 days)
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 ]

Answers (1)

Rik
Rik on 26 May 2020
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.

Community Treasure Hunt

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

Start Hunting!