フィルターのクリア

Replace 1's with 0's if there is less that five 1's between 0's in vector

2 ビュー (過去 30 日間)
Eric
Eric 2017 年 4 月 1 日
回答済み: the cyclist 2017 年 4 月 1 日
Hi everyone,
I want to replace 1's with 0's if there are less than five 1's between the zeros in a large vector. For example, I have the following vector:
x = [1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 1; 0];
And I would like to create a script that can find and replace only the 1's where there are less than five 0's in between, which would end up with the following results with the example from above:
x = [1; 1; 1; 1; 1; 1; 0; 0; 0; 0; 0; 1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 1; 0];
Thanks a lot in advance,
Eric

採用された回答

the cyclist
the cyclist 2017 年 4 月 1 日
Here is one way:
Download Jan Simon's RunLength code from the File Exchange. Then,
x = [1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 0; 1; 1; 1; 1; 1; 1; 1; 0];
[b n bi] = RunLength(x);
shortOneRunIndex = find(b'==1 & n<5);
for ns = shortOneRunIndex
x(bi(ns):bi(ns+1)-1) = 0;
end
This algorithm assumes that the sequence ends with a zero, as your example did. It could be fixed up to not require that assumption.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by