Azzera filtri
Azzera filtri

Gap fill using two vectors without a loop

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Sean de Wolski
Sean de Wolski il 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by