How to remove repeated neighbour numbers?

4 次查看(过去 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

采纳的回答

arich82
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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by