Gap fill using two vectors without a loop

2 次查看(过去 30 天)
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.

采纳的回答

Sean de Wolski
Sean de Wolski 2012-2-13
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by