searching first two consecutive ones and set to 0

2 次查看(过去 30 天)
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]

采纳的回答

Arif Hoq
Arif Hoq 2022-2-25
编辑:Arif Hoq 2022-2-25
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 个评论
Jan
Jan 2022-2-25
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)

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2022-2-25
编辑:Jan 2022-2-25
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 个评论
Jan
Jan 2022-2-25
As fas as I understand, this would be working then:
if all(y(1:2) == 1)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by