First we need a test data set:
> x=1:40; y=x*0; y(5:10)=1; y(15:20)=1; y(25:30)=1;y(33:36)=1;
> disp([x;y])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0
Now we find first
> BlockStart = find( diff(y)>0 )+1
BlockStart =
5 15 25 33
and last index of each non-zero block (note that numbers really coincide with the ones I used to make test data)
> BlockEnd = find( diff(y)<0 )
BlockEnd =
10 20 30 36
Then we just delete the gap between 3rd and 4th block in X (I rearranged the output):
> x( BlockEnd(3)+1 : BlockStart(4)-1) = []; % delete the gap in X
> y( BlockEnd(3)+1 : BlockStart(4)-1) = []; % delete the gap in Y
> disp([x;y])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 33 34 35 36 37 38 39 40
0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0
Also you may fill the gap in Y with ones instead of removing those elements:
> y( BlockEnd(3)+1 : BlockStart(4)-1) = 1;
> disp([x;y])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0