How can I count the frequency of each element of one vector?

2 ビュー (過去 30 日間)
yousef Yousef
yousef Yousef 2014 年 4 月 12 日
コメント済み: Azzi Abdelmalek 2014 年 4 月 13 日
Hi,I have this code which gives the number of occurrences of each element of w vector in vector y
a= [7 4 9 6 4 10 9 6 7 6]
y=zeros(size(a));
for i=1:length(a)
y(i)=sum(a==a(i));
end
y = y;
end
y=[2 2 2 3 2 1 2 3 2 3]
but this result is not enough because I need to know the index along with the number of repartitions.
the result should look like:
element no of repartitions position1 position2 position3
7 2 1 9 0
4 2 2 5 0
9 2 3 7 0
6 3 4 8 10
10 1 0 0 0

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 12 日
編集済み: Azzi Abdelmalek 2014 年 4 月 12 日
a= [7 4 9 6 4 10 9 6 7 6]
[b,idx]=unique(a)
[~,jdx]=sort(idx);
[ii,jj]=histc(a,b)
d=accumarray(jj',(1:numel(jj))',[],@(x) {[x' zeros(1,max(ii)-numel(x))]})
out=[b' ii' cell2mat(d)]
out=out(jdx,:)
  2 件のコメント
yousef Yousef
yousef Yousef 2014 年 4 月 12 日
Thanks but its not working .may you miss something
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 13 日
Look at edited answer

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by