How to remove repeated neighbour numbers?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have two A and B vectors, for example:
A=[9 9 8 7 6 6 6 7 9 9 10 9]
B=[1 1 2 3 1 2 3 2 3 2 4 5]
I want to keep only one number of each repeated value from A, and then make an average value from B.
At the end I want to get this:
Anew=[9 8 7 6 7 9 10 9]
Bnew=[1 2 3 2 2 2.5 4 5]
I do not want to lose the second/third 9 or second 7 of A.I just want to remove the repeated neighbour(s) value from A (with N numbers) and make an average of B.
Thank you
댓글 수: 0
채택된 답변
arich82
2015년 11월 13일
편집: arich82
2015년 11월 13일
We can construct a mask to get the non-repeated elements of A. Then, cumsum(mask) will effectively give a phase number for each "new" (i.e. non-repeated) entry in A, which we can use to accumulate B:
A=[9 9 8 7 6 6 6 7 9 9 10 9]
B=[1 1 2 3 1 2 3 2 3 2 4 5]
mask = [true, diff(A) ~= 0];
Anew = A(mask)
Bnew = accumarray(cumsum(mask).', B, [], @mean).'
output:
Anew =
9 8 7 6 7 9 10 9
Bnew =
1.0000 2.0000 3.0000 2.0000 2.0000 2.5000 4.0000 5.0000
Please accept this answer if it helps, or let me know in the comments if I've missed something.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!