MATLAB: How to filter out single/double zero’s

filterreplacementsmoothen

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

Best Answer

% 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;