From: Joshua on
Hi,

I was wondering if anyone knows a quicker way to compute the following:

C = A(A >= B(1) & A < B(2))

A is a large vector. B(1) is lower boundary B(2) is upper boundary.

I have this repeating in a loop several thousand times (each with different boundary conditions) in my code and this appears to be the bottle neck.

thanks,
J
From: us on
"Joshua" <joshua.edel(a)gmail.com> wrote in message <i1jt39$kf8$1(a)fred.mathworks.com>...
> Hi,
>
> I was wondering if anyone knows a quicker way to compute the following:
>
> C = A(A >= B(1) & A < B(2))
>
> A is a large vector. B(1) is lower boundary B(2) is upper boundary.
>
> I have this repeating in a loop several thousand times (each with different boundary conditions) in my code and this appears to be the bottle neck.
>
> thanks,
> J

this ...optimized... logical indexing construct will be hard (if not impossible) to beat using the regular ML engine...
did you look at a MEX solution(?)...

us
From: Oleg Komarov on
"Joshua" <joshua.edel(a)gmail.com> wrote in message <i1jt39$kf8$1(a)fred.mathworks.com>...
> Hi,
>
> I was wondering if anyone knows a quicker way to compute the following:
>
> C = A(A >= B(1) & A < B(2))
>
> A is a large vector. B(1) is lower boundary B(2) is upper boundary.
>
> I have this repeating in a loop several thousand times (each with different boundary conditions) in my code and this appears to be the bottle neck.
>
> thanks,
> J

Post the whole loop or use histc.

histc(A, boundaries)

Oleg
From: us on
"Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <i1juee$hld$1(a)fred.mathworks.com>...
> "Joshua" <joshua.edel(a)gmail.com> wrote in message <i1jt39$kf8$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I was wondering if anyone knows a quicker way to compute the following:
> >
> > C = A(A >= B(1) & A < B(2))
> >
> > A is a large vector. B(1) is lower boundary B(2) is upper boundary.
> >
> > I have this repeating in a loop several thousand times (each with different boundary conditions) in my code and this appears to be the bottle neck.
> >
> > thanks,
> > J
>
> Post the whole loop or use histc.
>
> histc(A, boundaries)
>
> Oleg

but... this certainly cannot be any faster as the OP would still need an additional (logical indexing based) command to retrieve the actual values...

us
From: us on
"us " <us(a)neurol.unizh.ch> wrote in message <i1juva$lq0$1(a)fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <i1juee$hld$1(a)fred.mathworks.com>...
> > "Joshua" <joshua.edel(a)gmail.com> wrote in message <i1jt39$kf8$1(a)fred.mathworks.com>...
> > > Hi,
> > >
> > > I was wondering if anyone knows a quicker way to compute the following:
> > >
> > > C = A(A >= B(1) & A < B(2))
> > >
> > > A is a large vector. B(1) is lower boundary B(2) is upper boundary.
> > >
> > > I have this repeating in a loop several thousand times (each with different boundary conditions) in my code and this appears to be the bottle neck.
> > >
> > > thanks,
> > > J
> >
> > Post the whole loop or use histc.
> >
> > histc(A, boundaries)
> >
> > Oleg
>
> but... this certainly cannot be any faster as the OP would still need an additional (logical indexing based) command to retrieve the actual values...
>
> us

the timing...

n=50000; % <- #trials
m=1:1000;
m=m(randperm(numel(m)));
ix=[5,m(end)-4];
tic;
for i=1:n
r1=m(m>=ix(1)&m<ix(2));
end
t1=toc;
ix(2)=ix(2)-1;
tic;
for i=1:n
[mx,mx]=histc(m,ix); %#ok
r2=m(mx~=0);
end
t2=toc;
disp(isequal(r1,r2))
disp([[{'t1'};{'t2'};{'gain'}],num2cell([t1;t2;t2/t1])]);
%{
% wintel sys: ic2/2*2.6gzh/2gb/winxp.sp3.32/r2010a.32
% note: data may vary due to RANDPERM...
1
't1' [0.6568]
't2' [1.1663]
'gain' [1.7758]
%}

us