Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Randomly sample from data, with or without replacement

`y = datasample(data,k)`

`y = datasample(data,k,dim)`

`y = datasample(___,Name,Value)`

`y = datasample(s,___)`

`[y,idx] = datasample(___)`

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.