Clear Filters
Clear Filters

Gap fill using two vectors without a loop

2 views (last 30 days)
Suppose I have a binary vector x and I want to fill gaps longer than a certain threshold. Is there a way to do this without a loop?
Example: x = [1 0 0 0 1 1 1 0 0 1 1 0 1]; th = 1;
I can get a,b which are vectors that represent the start and stop indices of the gaps. a = [2 8 12]; b = [4 9 12];
Right now I'm doing:
y=x;
excNdx = find( b-a > th );
for iNdx = excNdx
y(a(iNdx):b(iNdx)) = 1;
end
y = [1 1 1 1 1 1 1 1 1 1 1 0 1]
Also, any ideas for doing this down rows or columns of a matrix would be helpful also.

Accepted Answer

Sean de Wolski
Sean de Wolski on 13 Feb 2012
One of many ways:
x = [1 0 0 0 1 1 1 0 0 1 1 0 1];
th = 1; %greater than this
idx = strfind(x,zeros(1,th+1));
if ~isempty(idx)
x(bsxfun(@plus,idx,(0:th)')) = 1
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!