Thread Subject: Random number in matlab

Subject: Random number in matlab

From: Midhun

Date: 19 Aug, 2009 16:57:04

Message: 1 of 4


How can I perform a 'random selection' from an array in matlab ?

eg : if A = [1 , 2, 2, 2, 2, 3, 3, 4 , 4 , 4 ] , how can i randomly select 1 (or 2) elements from this array ?

If no bult-in command is available, this may be done by randomly selecting an array index between 1 and length(A) and then choose the corresponding element , but again, how can this be done :
   ie, randomly select 1 (or 2 ) numbers lying in a given interval.

Subject: Random number in matlab

From: arun

Date: 19 Aug, 2009 17:06:43

Message: 2 of 4

On Aug 19, 6:57 pm, "Midhun " <midhunj...@gmail.com> wrote:
> How can I perform a 'random selection' from an array in matlab ?
>
> eg : if   A = [1 , 2, 2, 2, 2,  3, 3, 4 , 4 , 4 ]  ,  how can i randomly select 1 (or 2) elements from this array ?  
>
> If no bult-in command is available, this may be done by randomly selecting an array index between 1 and length(A) and then choose the corresponding element  , but again, how can this be done :
>    ie, randomly select 1 (or 2 ) numbers lying in a given interval.

there are more than 1 way to do it.

1) help rand and help ceil
2) help randperm

best, arun.

Subject: Random number in matlab

From: Steven Lord

Date: 19 Aug, 2009 17:23:02

Message: 3 of 4


"Midhun " <midhunjoyp@gmail.com> wrote in message
news:h6hat0$s68$1@fred.mathworks.com...
>
> How can I perform a 'random selection' from an array in matlab ?
>
> eg : if A = [1 , 2, 2, 2, 2, 3, 3, 4 , 4 , 4 ] , how can i randomly
> select 1 (or 2) elements from this array ?
>
> If no bult-in command is available, this may be done by randomly selecting
> an array index between 1 and length(A) and then choose the corresponding
> element , but again, how can this be done :
> ie, randomly select 1 (or 2 ) numbers lying in a given interval.

Which version of MATLAB are you using?

Do you want to randomly select with repeats or without?

If you want to select without repeats, use RANDPERM. If you want to select
with repeats, either use RANDI (if you are using a recent-enough version of
MATLAB to have this function) or RAND and CEIL (if not.)

--
Steve Lord
slord@mathworks.com

Subject: Random number in matlab

From: Richard Willey

Date: 19 Aug, 2009 17:35:28

Message: 4 of 4

> If you want to select without repeats, use RANDPERM. If you want to
> select with repeats, either use RANDI (if you are using a recent-enough
> version of MATLAB to have this function) or RAND and CEIL (if not.)

Alternatively, Statistics Toolbox has a clever little function called
randsample
randsample
Random sample

Syntax
y = randsample(n,k)
y = randsample(population,k)
y = randsample(...,replace)
y = randsample(...,true,w)


Description
y = randsample(n,k) returns a k-by-1 vector y of values sampled uniformly at
random, without replacement, from the integers 1 to n.

y = randsample(population,k) returns a vector of values sampled uniformly at
random, without replacement, from the values in the vector population. The
orientation of y (row or column) is the same as population.

y = randsample(...,replace) returns a sample taken with replacement if
replace is true, or without replacement if replace is false. The default is
false.

y = randsample(...,true,w) returns a weighted sample taken with replacement,
using a vector of positive weights w, whose length is n. The probability
that the integer i is selected for an entry of y is w(i)/sum(w). Usually, w
is a vector of probabilities. randsample does not support weighted sampling
without replacement.

Examples
The following command generates a random sequence of the characters A, C, G,
and T, with replacement, according to the specified probabilities.

R = randsample('ACGT',48,true,[0.15 0.35 0.35 0.15])

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
random number Midhun 19 Aug, 2009 12:59:22
matlab Midhun 19 Aug, 2009 12:59:22
rssFeed for this Thread

Contact us at files@mathworks.com