フィルターのクリア

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 Center および File ExchangeStatistics 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