Q:
Suppose
a = [0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 0 1 ];
I'd like to organize this vector into a matrix with the following criteria: every time I see a 1,
I want to copy that 1 and 2 other members before it into a new line, so my resulting matrix will look like this:
b = [
0 0 1
0 1 1
0 0 1
0 0 1
0 0 1
1 0 1];
Is there a vectorized solution?
A:
apad=[0 0 a];
i1 = find(apad == 1);
b = apad(bsxfun(@plus, i1(:), (-2:0)))
No comments:
Post a Comment