On Jun 13, 8:03 pm, "Jack Glenn" <sonatine2...@yahoo.com> wrote:
> All,
>
> I simulated 50,000 random numbers (r) and wanted to compare them with a prespecified strictly decreasing sequence (dec_arr, with length 300). For each of the 50,000, I'll need to find the first in the sequence of 300 such that it falls below the random number, and return the index. I know that r is always between the minimum and maximum of dec_arr. I wrote something like this:
>
> N=length(r); % N = 50,000
> idx = nan(N,1);
> for i = 1:N
> idx[i] = find(r(N) > dec_arr, 1, 'first');
> end
>
> but it wasn't fast enough. My question is how I can do this faster. I tried repmat to create r_rep (50000*300) and dec_arr (50000*300), and use vectorized operation sum(r_rep<=dec_arr, 2). The thing is sum is fast, but the two repmat's are slow, and overall it's even slower than the find loop I did above.
>
> Given that dec_arr is decreasing, is there any faster way to achieve what I want?
>
> Thanks for your help.
>
>  Jack
One of the solutions;
% data;
r = rand(50000,1);
d = linspace(1,0,300); % same as dec_arr
% engine;
[dump,idx] = histc(r,d(end:1:1));
idx = length(d)idx+1;
