Combining duplicate entries in a data array
조회 수: 7 (최근 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!