From: Oleg Komarov on
> > 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

Definitely right.

Oleg
From: Cris Luengo 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

There might be a speed-up if you tell us what it is that you do with C. But that statement, as it is, is as fast as you can make it with MATLAB. As somebody else suggested, a MEX-file (for the whole loop) will most probably increase the speed.

Cheers,
Cris.
From: Joshua on
thanks for all your comments. The complete code I have written is as follows. If you think this could be optimized further, do let me know.

regards,
Joshua


Width = 512;
Length = 512;
BinW = 4;
BinL = 4;


Wd_bin = Width/BinW;
DB.Start = 5;
DB.End = 594;
MXch = DB.End;

p = 1;

for i = 6
if rem(i-1,BinL) == 0
Decay = zeros(MXch,Wd_bin);
Im_1 = zeros(1,Wd_bin);
DecayAll = zeros(MXch,Wd_bin);
Img = zeros(1,Wd_bin);
p = p +1;
end

for j = 1:10
Fm_1 = MacroT(MacroT >= Mf(j) & MacroT < Mf(j+1))*1e-7;
ch_1 =ch(MacroT >= Mf(j) & MacroT < Mf(j+1));
M_1 = Marker_1(Marker_1 >= Mf(j) & Marker_1 < Mf(j+1))*1e-7;
M_2 = Marker_2(Marker_2 >= Mf(j) & Marker_2 < Mf(j+1))*1e-7;

T_pix = (M_1(i+1) - M_2(i))/(Wd_bin);
Fm_2 = Fm_1(Fm_1 >= M_2(i) & Fm_1 < M_1(i+1));
ch_2 = ch_1(Fm_1 >= M_2(i) & Fm_1 < M_1(i+1));

if ~isempty(Fm_2)
[Im_1(1,:), binRow] = histc(Fm_2, M_2(i):T_pix:M_1(i+1)-T_pix);

ch_2(ch_2 == 0) = 1;
ch_2(binRow == 0) = [];
binRow(binRow == 0) = [];
Mat = sub2ind([MXch Wd_bin], ch_2, binRow);
[Rep,I] = histc(Mat,min(Mat):max(Mat));
Rep(Rep == 0) = [];
p = unique(Mat);
Decay(p) = Rep;
RowDecay = Decay;

DecayAll = DecayAll + RowDecay; %
Img(1,:) = Img(1,:) + Im_1(1,:);
end
end
end
From: us on
"Joshua"
> ch_2(binRow == 0) = [];
> binRow(binRow == 0) = [];

a first glance at the idiosyncratic code shows this (slight) possibility of improvement

ix=binRow==0; % logical indexing needs to be done only once...
ch_2(ix)=[];
binRow(ix)=[];

us
From: Jan Simon on
Dear Joshua,

> [Rep,I] = histc(Mat,min(Mat):max(Mat));
If you do not need [I], omit it.

> Img(1,:) = Img(1,:) + Im_1(1,:);
If Img is a vector, omit the index:
Img = Img + Im_1(1,:);

Are you sure the multiplication by 1e-7 is necessary?

How large are MacroT, Marker_1 and Marker_2?
If they are large, create a Mex for "A(A >= a & A < b)".

Kind regards, Jan