How to delete ones in a binary vector that correspond to more than k consecutive repetitions?
2 ビュー (過去 30 日間)
古いコメントを表示
GONZALEZ DE COSSIO ECHEVERRIA Francisco Jose
2020 年 4 月 27 日
コメント済み: Ameer Hamza
2020 年 4 月 29 日
Given a number k and a binary vector v. How to saturate the number of consecutive repetitions of ones in v to the value k? For example, if k=3 and v=[1,0,0,1,1,0,1,1,1,1,0,0] then the output should be w=[1,0,0,1,1,0,1,1,1,0,0]. Speed of the code is highly important and no toolboxes available.
Thanks in advance
0 件のコメント
採用された回答
Ameer Hamza
2020 年 4 月 27 日
編集済み: Ameer Hamza
2020 年 4 月 27 日
Try this. It uses function from image processing toolbox. You can check if the speed is acceptable
v = [1,0,0,1,1,0,1,1,1,1,0,0,1,1,1,1,1,0,1];
rgs = bwconncomp(v);
pxlList = cellfun(@(x) {(x(4:end).')}, rgs.PixelIdxList);
v([pxlList{:}]) = [];
Result:
v =
Columns 1 through 13
1 0 0 1 1 0 1 1 1 0 0 1 1
Columns 14 through 16
1 0 1
4 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!