1。程序实现
设数据存放于向量a中,需要重复n次简单随机采样,程序及说明如下:
m=length(a); %dimension
idx= ceil(m*rand(1,n)) ; %generate n random index between 1 and m
b = a(idx) ; % sampling
2。自带函数
RANDSAMPLE Random sample, with or without replacement.
Y = RANDSAMPLE(N,K) returns Y as a vector of K values sampled
uniformly at random, without replacement, from the integers 1:N.
Y = RANDSAMPLE(POPULATION,K), where POPULATION is a vector of two or more
values, returns K values sampled uniformly at random, without replacement,
from the values in the vector POPULATION.
Y = RANDSAMPLE(...,REPLACE) returns a sample taken with replacement if
REPLACE is true, or without replacement if REPLACE is false (the default).
Y = RANDSAMPLE(...,true,W) returns a weighted sample, using positive
weights W, taken with replacement. W is often a vector of probabilities.
This function does not support weighted sampling without replacement.
Y = RANDSAMPLE(S, ...) uses the random stream S for random number
generation. S is a random stream created using RandStream.
Default is the MATLAB default random number stream.
Example: Generate a random sequence of the characters ACGT, with
replacement, according to specified probabilities.
R = randsample('ACGT',48,true,[0.15 0.35 0.35 0.15])
See also rand, randperm, RandStream.