필터 지우기
필터 지우기

searching first two consecutive ones and set to 0

조회 수: 2 (최근 30일)
VASUNDHARA V
VASUNDHARA V 2022년 2월 25일
댓글: VASUNDHARA V 2022년 2월 25일
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
Arif Hoq
Arif Hoq 2022년 2월 25일
my pleasure
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)
VASUNDHARA V
VASUNDHARA V 2022년 2월 25일
yes sir

댓글을 달려면 로그인하십시오.

카테고리

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