Counting the number of occurences for specific values in an array

1 visualización (últimos 30 días)
Shady Elsayed
Shady Elsayed el 26 de Mayo de 2020
Respondida: Rik el 26 de Mayo de 2020
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 ]

Respuestas (1)

Rik
Rik el 26 de Mayo de 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.

Categorías

Más información sobre Histograms en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by