Need to remove repeated adjacent elements in an array
Mostrar comentarios más antiguos
I need to turn
[1 1 1 1 2 2 2 6 6 6 6 2 2 2 2] into [1 2 6 2]
unique() gives [1 2 6], but I want to preserve the second value
any advice?
3 comentarios
Michael Cappello
el 15 de Mayo de 2015
x(diff(x)==0) = []
Matthew Rademacher
el 16 de Mayo de 2015
Ravi Mravi
el 30 de Oct. de 2017
Excellent solution
Respuesta aceptada
Más respuestas (2)
Joseph Cheng
el 15 de Mayo de 2015
Editada: Joseph Cheng
el 15 de Mayo de 2015
you can use diff to determine the consecutive same value numbers
test = [1 1 1 1 2 2 2 6 6 6 6 2 2 2 2]
mtest = [test test(end)-1];
difftest = diff(mtest)
output = test(difftest~=0)
the mtest is the modified test number to get the last value not the same. if you look at the output of difftest you see that we get the positions of the transitions from one number to another.
Image Analyst
el 15 de Mayo de 2015
Here's one way:
m = [1 1 1 1 2 2 2 6 6 6 6 2 2 2 2]
logicalIndexes = [0, diff(m)] ~= 0
output = [m(1), m(logicalIndexes)]
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!