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