Borrar filtros
Borrar filtros

How to remove repeated neighbour numbers?

2 visualizaciones (últimos 30 días)
Amin Gan
Amin Gan el 13 de Nov. de 2015
Comentada: Amin Gan el 13 de Nov. de 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

Respuesta aceptada

arich82
arich82 el 13 de Nov. de 2015
Editada: arich82 el 13 de Nov. de 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.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by