From: Tom Toner on
Hi,

I have a 1x10,000 array of random 0s and 1s. I was trying to find the 1s between 0s at the odd positions and 0s at the even positions and discard the 1s between 0s at even positions and the odd positions. For example, for [ 0 0 1 0 1 1 0 1 0 ], [ 1 1] at ind=5 & 6 are the ones I want to find.

Since the arrary is very large, I was wondering if there was any way to find these 1s without using loops.

Thanks,
From: Bruno Luong on
"Tom Toner" <nm2bean(a)yahoo.com> wrote in message <hlvpcc$f95$1(a)fred.mathworks.com>...
> Hi,
>
> I have a 1x10,000 array of random 0s and 1s. I was trying to find the 1s between 0s at the odd positions and 0s at the even positions and discard the 1s between 0s at even positions and the odd positions. For example, for [ 0 0 1 0 1 1 0 1 0 ], [ 1 1] at ind=5 & 6 are the ones I want to find.
>
> Since the arrary is very large, I was wondering if there was any way to find these 1s without using loops.
>
> Thanks,

Use can use the function SplitVec on FEX
http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter

a=[ 0 0 1 0 1 1 0 1 0 ]

[f l e i] = SplitVec(a,[],'first','last','firstelem','loc');
removed = e(:)==1 & mod(f,2)==1 & mod(l,2)==0;
a([i{removed}])=[]

% Bruno
From: Tom Toner on
Thanks a lot, Bruno!

It's a great function!

Tom
From: us on
"Tom Toner" <nm2bean(a)yahoo.com> wrote in message <hlvpcc$f95$1(a)fred.mathworks.com>...
> Hi,
>
> I have a 1x10,000 array of random 0s and 1s. I was trying to find the 1s between 0s at the odd positions and 0s at the even positions and discard the 1s between 0s at even positions and the odd positions. For example, for [ 0 0 1 0 1 1 0 1 0 ], [ 1 1] at ind=5 & 6 are the ones I want to find.
>
> Since the arrary is very large, I was wondering if there was any way to find these 1s without using loops.
>
> Thanks,

one of the many solutions

% the data
v=[0 0 1 0 1 1 0 1 0 0 0 1 0 1 1 0 1 1 0];
% the engine
ix=regexp(char(b+'0'),'01{2,}')+1;
ix=ix(bitand(ix,1)~=0);
% the result
disp(ix);
% 5 17

us
From: Tom Toner on
Hi US,

It is interesting to see your code. I had also thought of using regexp but couldn't figure it out.

There is a little problem of your codelet. The 1 at ind=12 is missing. Could you please fix it?

Thanks,
Tom