Borrar filtros
Borrar filtros

searching first two consecutive ones and set to 0

2 visualizaciones (últimos 30 días)
VASUNDHARA V
VASUNDHARA V el 25 de Feb. de 2022
Comentada: VASUNDHARA V el 25 de Feb. de 2022
y=[1 1 1 1 1 1 1 1 1 1 1]
i want to search for first two consecutive ones everytime and allocate them 0
like this
y=[0 0 1 1 1 1 1 1 1 1 1]

Respuesta aceptada

Arif Hoq
Arif Hoq el 25 de Feb. de 2022
Editada: Arif Hoq el 25 de Feb. de 2022
try this:
y=[1 1 1 1 1 1 1 1 1 1 1];
idx=y(1:2);
b=find(y(idx));
if y(b)==1
y(b)=0;
end
disp(y)
0 0 1 1 1 1 1 1 1 1 1
  3 comentarios
Arif Hoq
Arif Hoq el 25 de Feb. de 2022
my pleasure
Jan
Jan el 25 de Feb. de 2022
This does not work, if y does not start ith two 1 values:
y=[0 0 1 1 1 1 1 1 1 1 1]
y = 1×11
0 0 1 1 1 1 1 1 1 1 1
idx=y(1:2);
b=find(y(idx));
Array indices must be positive integers or logical values.
if y(b)==1
y(b)=0;
end
disp(y)

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 25 de Feb. de 2022
Editada: Jan el 25 de Feb. de 2022
y = [0 0 1 1 1 1 1 1 1 1 1];
index = strfind(y, [1, 1]);
if any(index)
y(index(1):index(1)+1) = 0;
end
y
y = 1×11
0 0 0 0 1 1 1 1 1 1 1
  3 comentarios
Jan
Jan el 25 de Feb. de 2022
As fas as I understand, this would be working then:
if all(y(1:2) == 1)

Iniciar sesión para comentar.

Categorías

Más información sobre Statistics and Machine Learning Toolbox 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