How to remove repeated neighbour numbers?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amin Gan
am 13 Nov. 2015
Kommentiert: Amin Gan
am 13 Nov. 2015
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 Kommentare
Akzeptierte Antwort
arich82
am 13 Nov. 2015
Bearbeitet: arich82
am 13 Nov. 2015
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!