How to filter out single/double zero's ?

조회 수: 1 (최근 30일)
JamJan
JamJan 2019년 9월 23일
댓글: Guillaume 2019년 9월 23일
A = [1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
I have vector A and I want to smoothen the vector by changing the single 0's or double 0's to 1's. This means that I want to keep the consecutive zero's when bigger than 2 consecutive.
Is there an easy way to do this without using a for loop?
Thanks

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 9월 23일
% find indices
idx1 = strfind([0 A == 0 0],[0 1 0]);
idx2 = strfind([0 A == 0 0],[0 1 1 0]);
% replace
A(idx1) = 1;
A([idx2(:); idx2(:)+1]) = 1;
  댓글 수: 1
Guillaume
Guillaume 2019년 9월 23일
Note that, while this is a good solution, this usage of strfind with numeric arrays is undocumented. I have suggested to Mathworks to make it documented (or make a function that does the same for numeric vectors), but for now, use at your own perils. It may stop working in future versions.

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

추가 답변 (2개)

Matt J
Matt J 2019년 9월 23일
편집: Matt J 2019년 9월 23일
B=char([1 A 1]+'0');
B=strrep(B, '101','111');
B=strrep(B,'1001','1111');
B=B(2:end-1)-'0';

Guillaume
Guillaume 2019년 9월 23일
If you have the Image Processing Toolbox, the easiest is:
newA = imclose(A, [1 1 1])

카테고리

Help CenterFile Exchange에서 Global or Multiple Starting Point Search에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by