Help with removing duplicate sub-elements....

1 visualización (últimos 30 días)
Sowmya MR
Sowmya MR el 6 de Feb. de 2016
Comentada: Sowmya MR el 6 de Feb. de 2016
Hi, I have a vector a1 and want to get an output a2. Basically, i should identify the starting index of numbers, keep the first value and replace the repeating elements as NaN. I have no idea how to start since i am new to Matlab. Can someone please help?
a1=[-2 -2 -2 -2 NaN NaN NaN -3 -3 -3 -3 NaN NaN -2 -2 -2 NaN -3 -3 NaN NaN 1 1 1 1 NaN NaN 4 4 4 NaN NaN 3 3 3 NaN];
a2=[-2 NaN NaN NaN NaN NaN NaN -3 NaN NaN NaN NaN NaN -2 NaN NaN NaN -3 NaN NaN NaN 1 NaN NaN NaN 4 NaN NaN NaN NaN 3 NaN NaN NaN];

Respuesta aceptada

Stephen23
Stephen23 el 6 de Feb. de 2016
Editada: Stephen23 el 6 de Feb. de 2016
Use isnan and diff to create some indices, then replace those values:
>> A = [-2 -2 -2 -2 NaN NaN NaN -3 -3 -3 -3 NaN NaN -2 -2 -2 NaN -3 -3 NaN NaN 1 1 1 1 NaN NaN 4 4 4 NaN NaN 3 3 3 NaN]
A =
-2 -2 -2 -2 NaN NaN NaN -3 -3 -3 -3 NaN NaN -2 -2 -2 NaN -3 -3 NaN NaN 1 1 1 1 NaN NaN 4 4 4 NaN NaN 3 3 3 NaN
>> B = A;
>> B(-1<diff(isnan([NaN,A]))) = NaN
B =
-2 NaN NaN NaN NaN NaN NaN -3 NaN NaN NaN NaN NaN -2 NaN NaN NaN -3 NaN NaN NaN 1 NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN 3 NaN NaN NaN
  3 comentarios
Stephen23
Stephen23 el 6 de Feb. de 2016
Editada: Stephen23 el 6 de Feb. de 2016
@Sowmya MR: My code only depends on the NaN's, not the other values. Try it.
Sowmya MR
Sowmya MR el 6 de Feb. de 2016
Awesome. Thank you. Works like a gem

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by