필터 지우기
필터 지우기

Gap fill using two vectors without a loop

조회 수: 2 (최근 30일)
Donald
Donald 2012년 2월 13일
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