Counting the number of occurences for specific values in an array

1 回表示 (過去 30 日間)
Shady Elsayed
Shady Elsayed 2020 年 5 月 26 日
回答済み: Rik 2020 年 5 月 26 日
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 Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by