Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to find by column efficiently?

Subject: How to find by column efficiently?

From: Jack Glenn

Date: 14 Jun, 2009 03:03:02

Message: 1 of 3

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

Subject: How to find by column efficiently?

From: Siyi

Date: 14 Jun, 2009 05:01:36

Message: 2 of 3

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 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


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;

Subject: How to find by column efficiently?

From: Jack Glenn

Date: 14 Jun, 2009 21:24:01

Message: 3 of 3

This is lightning fast. What a beautiful solution. Thank you Siyi!

Siyi <Mr.Siyi.Deng@gmail.com> wrote in message <dfbfe26d-090c-4ecd-81fa-76e37b60001c@v35g2000pro.googlegroups.com>...

> 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;

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us