Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find by column efficiently?

0 views
Skip to first unread message

Jack Glenn

unread,
Jun 13, 2009, 11:03:02 PM6/13/09
to
All,

I simulated 50,000 random numbers (r) and wanted to compare them with a pre-specified 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

Siyi

unread,
Jun 14, 2009, 1:01:36 AM6/14/09
to


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;


Jack Glenn

unread,
Jun 14, 2009, 5:24:01 PM6/14/09
to
This is lightning fast. What a beautiful solution. Thank you Siyi!

Siyi <Mr.Siy...@gmail.com> wrote in message <dfbfe26d-090c-4ecd...@v35g2000pro.googlegroups.com>...

0 new messages