Effacer les filtres
Effacer les filtres

How to remove repeated neighbour numbers?

2 vues (au cours des 30 derniers jours)
Amin Gan
Amin Gan le 13 Nov 2015
Commenté : Amin Gan le 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

Réponse acceptée

arich82
arich82 le 13 Nov 2015
Modifié(e) : arich82 le 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.
  1 commentaire
Amin Gan
Amin Gan le 13 Nov 2015
Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by