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:
Drawing n random numbers from vector without replacement

Subject: Drawing n random numbers from vector without replacement

From: Ulrik Nash

Date: 8 Jul, 2010 08:05:06

Message: 1 of 3

Hi Everyone,

Suppose I wish to draw 2 numbers at random without replacement from vector A

A = [1 2 3 4 5 6 7 8 9 10]

Which command will let me do this in Matlab?

Best,

Ulrik.

Subject: Drawing n random numbers from vector without replacement

From: Jan Simon

Date: 8 Jul, 2010 08:29:04

Message: 2 of 3

Dear Ulrik,

> Suppose I wish to draw 2 numbers at random without replacement from vector A
> A = [1 2 3 4 5 6 7 8 9 10]
> Which command will let me do this in Matlab?

See RANDPERM: Permute the vector randomly and draw the first 2 numbers.

If you need speed, see:
  http://www.mathworks.com/matlabcentral/fileexchange/27076
If your A is large, e.g. 1e7 elements, the random permutation wastes a remarkable chunk of time and memory. SHUFFLE in the "index" mode is more efficient:
  A = rand(1, 1e7);
  Index = Shuffle(1e7, 'index', 2);
  B = A(Index)

Good luck, Jan

Subject: Drawing n random numbers from vector without replacement

From: Ulrik Nash

Date: 8 Jul, 2010 10:08:03

Message: 3 of 3

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1428g$q8d$1@fred.mathworks.com>...
> Dear Ulrik,
>
> > Suppose I wish to draw 2 numbers at random without replacement from vector A
> > A = [1 2 3 4 5 6 7 8 9 10]
> > Which command will let me do this in Matlab?
>
> See RANDPERM: Permute the vector randomly and draw the first 2 numbers.
>
> If you need speed, see:
> http://www.mathworks.com/matlabcentral/fileexchange/27076
> If your A is large, e.g. 1e7 elements, the random permutation wastes a remarkable chunk of time and memory. SHUFFLE in the "index" mode is more efficient:
> A = rand(1, 1e7);
> Index = Shuffle(1e7, 'index', 2);
> B = A(Index)
>
> Good luck, Jan



Thanks Jan, that was a great help.

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