MATLAB: Set logical array values to true if value n values ahead is true

logical-arraysvectorization

I'm curious if anyone can find a smart/clean way (other than a for loop of doing the following). Given a logical array x:
x = logical( [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0] );
For n = 1 I'd like: y = [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0];
For n = 2 I'd like: y = [0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0];
etc.

Best Answer

x = [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0] > 0;
n = 2;
x(bsxfun(@minus,find(x),(n:-1:1)')) = true;