From: Tom Toner on
Hi Siyi,

Thanks a lot for the imporved code. You've doubled the calculation speed.

Tom

Deng <mr.siyi.deng(a)gmail.com> wrote in message <13842c0e-052c-4acf-9181-ba2452e44d3a(a)s36g2000prf.googlegroups.com>...
> On Feb 25, 12:28 pm, "Tom Toner" <nm2b...(a)yahoo.com> wrote:
> > Hi
> >
> > I have a vector
> >       v=1:20;
> > and two index vectors
> >       i1=[3 6 11 15];
> >       i2=[5 9 12 19];
> >
> > I am trying to find the elements between i1(k) and i2(k), k=1:4.
> > The results should be [4 7 8 16 17 18].
> >
> > Could you please help me do it without loops?
> > Thanks,
> > Tom
>
> s = (i2-i1) > 1;
> x = zeros(size(v));
> x(i1(s)+1) = 1;
> x(i2(s)) = -1;
>
> index = logical(cumsum(x));
>
> v(index)
>