how to obtain the id of change in vector

1 visualización (últimos 30 días)
Niraj
Niraj el 27 de Nov. de 2012
i have a vector say
v =[ 0 0 0 0 28 34 0 56 67 0 0 0 69 71 75 89 90 0]
I need to find what are the ids where there is change in value from 0 to some number or from some number to 0. So in this case the ids should be like 5 6 8 9 13 17
Is there a smart way to do it?

Respuestas (2)

Walter Roberson
Walter Roberson el 27 de Nov. de 2012
Lv = (v ~= 0);
find( Lv(1:end-1) ~= Lv(2:end) )
  3 comentarios
Jan
Jan el 27 de Nov. de 2012
I assume "id" means "index".
Niraj
Niraj el 27 de Nov. de 2012
yes,"id" means "index".

Iniciar sesión para comentar.


Jan
Jan el 27 de Nov. de 2012
Editada: Jan el 27 de Nov. de 2012
Lv = (v ~= 0);
Index = find(diff(Lv)) + 1;
[EDITED]
Lv = [false, (v ~= 0), false];
ini = strfind(Lv, [false, true]) + 1;
fin = strfind(Lv, [true, false]);
Index = reshape([ini(:), fin(:)], 1, []);
  2 comentarios
Niraj
Niraj el 27 de Nov. de 2012
Editada: Niraj el 27 de Nov. de 2012
Thanks for the answer, but it gives the output as
5 7 8 10 13 18
but i want
5 6 8 9 13 17
Jan
Jan el 27 de Nov. de 2012
See [EDITED]

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by