フィルターのクリア

Counting occurrence of elements in an array

88 ビュー (過去 30 日間)
Mustafa Sheikh
Mustafa Sheikh 2015 年 3 月 25 日
コメント済み: AB WAHEED LONE 2021 年 3 月 26 日
How would I go about counting the occurrence of elements in an array including elements that may not be in the array.
For example if x = [2 3 2 4 5 6 8 2 9 5], I would like to produce an array that has the frequency of each element from 1 to 10 so it'd be output = [0 3 1 1 2 1 0 1 1 0]
Thanks

採用された回答

Star Strider
Star Strider 2015 年 3 月 25 日
編集済み: Star Strider 2015 年 3 月 25 日
Use the hist function:
x = [2 3 2 4 5 6 8 2 9 5];
binc = [1:10];
counts = hist(x,binc);
result = [binc; counts]
produces:
result =
1 2 3 4 5 6 7 8 9 10
0 3 1 1 2 1 0 1 1 0
  1 件のコメント
AB WAHEED LONE
AB WAHEED LONE 2021 年 3 月 26 日
That is great.
It works

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

その他の回答 (1 件)

Ryan
Ryan 2015 年 3 月 25 日
編集済み: Ryan 2015 年 3 月 25 日
This works....
x= [2 3 2 4 5 6 8 2 9 5]
for index=1:length(x)
y(index)= sum(x==index);
end
y
produces
x =
2 3 2 4 5 6 8 2 9 5
y =
0 3 1 1 2 1 0 1 1 0

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by