フィルターのクリア

How to sum frequency of the duplicate value in an array?

1 回表示 (過去 30 日間)
tedy
tedy 2013 年 4 月 7 日
Hello,
i have two array g=[1,3,2,2,5,4,4] and h=[2,4,5,2,7,5,3]. g is the value and h is the frequency. what i wanna do is the duplicate value only have one value and also the frequency of the duplicate value will be summed. it's like g=[1,3,2,5,4] and h=[2,4,7,7,8]. i try this code to delete the duplicate
i = g([diff(g)~=0, true]);
but, i don't know how to sum the frequency of the duplicate value? please help me.
Thanks in advance

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 7 日
編集済み: Azzi Abdelmalek 2013 年 4 月 7 日
[a,b]=unique(g)
new_g=g(sort(b))
new_h=arrayfun(@(x) sum(h(g==x)),new_g)
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 7 日
Or if you have new version of Matlab
[new_g,~]=unique(g,'stable')
new_h=arrayfun(@(x) sum(h(g==x)),new_g)

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

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by