Main Content

Randomly sample from data, with or without replacement

returns a sample for any of the input arguments in the previous syntaxes, with additional options specified by one or more name-value pair arguments. For example, `y`

= datasample(___,`Name,Value`

)`'Replace',false`

specifies sampling without replacement.

`datasample`

uses `randperm`

, `rand`

, or `randi`

to generate random values. Therefore, `datasample`

changes the state of the MATLAB^{®} global random number generator. Control the random number generator using `rng`

.

For selecting weighted samples without replacement, `datasample`

uses the algorithm of Wong and Easton [1].

You can use `randi`

or `randperm`

to generate indices for random sampling with or without replacement, respectively. However, `datasample`

can be more convenient to use because it samples directly from your data. `datasample`

also allows weighted sampling.

[1] Wong, C. K. and M. C. Easton. "An Efficient Method for Weighted Sampling Without Replacement." *SIAM Journal of Computing* 9(1), pp. 111–113, 1980.