Filter löschen
Filter löschen

Gap fill using two vectors without a loop

2 Ansichten (letzte 30 Tage)
Donald
Donald am 13 Feb. 2012
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.

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by