Combining duplicate entries in a data array
8 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have two vectors x and y with corresponding data points. x is in sorted order, but may have double entries, e.g.
x = [0.1, 5, 5, 5, 14.5, 16, 16, 21];
y = [70, 8, 2, 3.5, 6, 5, 2.5, 3.3];
What I would like to do is find the double entries of x and combine the corresponding y values (by summing them up), so that
x_new = [0.1, 5, 14.5, 16, 21];
y_new = [70, 8+2+3.5, 6, 5+2.5, 3.3] = [70, 13.5, 7.5, 3.3];
However I struggle to manage this sufficiently. I have tried to use the function
[x_new, ia, ic] = unique(x);
but I don't know how to efficiently use these values further. Any ideas? :)
Thanks for your help!
0 件のコメント
採用された回答
Stephen23
2018 年 3 月 14 日
編集済み: Stephen23
2018 年 3 月 14 日
>> x = [0.1, 5, 5, 5, 14.5, 16, 16, 21];
>> y = [70, 8, 2, 3.5, 6, 5, 2.5, 3.3];
>> [xnew,~,idx] = unique(x);
>> ynew = accumarray(idx(:),y(:))
ynew =
70.0000
13.5000
6.0000
7.5000
3.3000
2 件のコメント
Batuhan Arik
2021 年 10 月 7 日
Hello, did the exact same thing, it seems to be working. I get x_new and y_new as they are supposed to come out. However, when I try
bar(x_new,y_new);
the plot comes out empty.

その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!