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:
Random numbers and arrays (part 2)

Subject: Random numbers and arrays (part 2)

From: Ulrik Nash

Date: 13 Dec, 2009 16:08:03

Message: 1 of 10

How do I select n numbers at random without replacement from an array with m numbers, where n >=m?

Subject: Random numbers and arrays (part 2)

From: dpb

Date: 13 Dec, 2009 16:35:33

Message: 2 of 10

Ulrik Nash wrote:
> How do I select n numbers at random without replacement from an array
> with m numbers, where n >=m?

If n>m, can't w/o replacement as there aren't enough in the pool
If n==m, take the entire pool

--

Subject: Random numbers and arrays (part 2)

From: Ulrik Nash

Date: 13 Dec, 2009 16:48:02

Message: 3 of 10

Should read n<=m.

Subject: Random numbers and arrays (part 2)

From: Matt Fig

Date: 13 Dec, 2009 17:53:03

Message: 4 of 10


% Data
n = 5; % Take n numbers
A = round(rand(4,9)*200); % An array

% Engine
m = numel(A);
B = randperm(m);
C = A(B(1:n))

Subject: Random numbers and arrays (part 2)

From: dpb

Date: 13 Dec, 2009 18:02:07

Message: 5 of 10

Ulrik Nash wrote:
> Should read n<=m.

p=[1:m]; % the pool
r=p(randperm(n)); % a randomly selected subset

--

Subject: Random numbers and arrays (part 2)

From: Bruno Luong

Date: 13 Dec, 2009 19:14:09

Message: 6 of 10

dpb <none@non.net> wrote in message <hg3a7l$mke$1@news.eternal-september.org>...


% a randomly selected subset
>

of n first elements?

Bruno

Subject: Random numbers and arrays (part 2)

From: dpb

Date: 13 Dec, 2009 19:53:00

Message: 7 of 10

Bruno Luong wrote:
> dpb <none@non.net> wrote in message <hg3a7l$mke$1@news.eternal-september.org>...
>
>
> % a randomly selected subset
>
> of n first elements?

n randomly selected values from 1:m, yeah...why not? (unless I'm
overlooking something) -- oh, I am, indeed, sorry...

OK, ignore the above "solution"... :(

--

Subject: Random numbers and arrays (part 2)

From: Steven Lord

Date: 14 Dec, 2009 04:30:43

Message: 8 of 10


"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
news:hg33h3$7jd$1@fred.mathworks.com...
> How do I select n numbers at random without replacement from an array with
> m numbers, where n >=m?

Others have suggested RANDPERM, but if m is much larger than n, you could
save some memory using a rejection method:

n = 10;
m = 1e6;
selection = randi(m, [1, n]);
while numel(unique(selection)) < n
    selection = randi(m, [1 n]);
end

If your version of MATLAB does not have RANDI, multiply RAND by a scale
factor and round.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Random numbers and arrays (part 2)

From: Peter Perkins

Date: 14 Dec, 2009 15:00:19

Message: 9 of 10

Steven Lord wrote:
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
> news:hg33h3$7jd$1@fred.mathworks.com...
>> How do I select n numbers at random without replacement from an array with
>> m numbers, where n >=m?
>
> Others have suggested RANDPERM, but if m is much larger than n, you could
> save some memory using a rejection method:

Or if you have access to the Statistics Toolbox, RANDSAMPLE is the way to go.

Subject: Random numbers and arrays (part 2)

From: Akim

Date: 15 Dec, 2009 06:49:02

Message: 10 of 10

Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <hg5ju3$mt4$1@fred.mathworks.com>...
>
> Or if you have access to the Statistics Toolbox, RANDSAMPLE is the way to go.

Here's a way that works much faster for me (cf. randsample):

m=numel(array);
n1=round(n*f);
u=unique(ceil(m*rand(n1,1)));
sample=array(u(1:n));

Where f>1 is sufficiently large. E.g.: http://www.mathworks.com/matlabcentral/newsreader/view_thread/268343#702300

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