Categories &

Functions List

Function Reference: datasample

statistics: y = datasample (data, k)

statistics: y = datasample (data, k, dim)

statistics: y = datasample (…, Name, Value)

statistics: [y idcs] = datasample (…)

Randomly sample data.

Return k observations randomly sampled from data. data can be a vector or a matrix of any data. When data is a matrix or a n-dimensional array, the samples are the subarrays of size n - 1, taken along the dimension dim. The default value for dim is 1, that is the row vectors when sampling a matrix.

Output y is the returned sampled data. Optional output idcs is the vector of the indices to build y from data.

Additional options are set through pairs of parameter name and value. Available parameters are:

Replace
a logical value that can be true (default) or false: when set to true, datasample returns data sampled with replacement.
Weights
a vector of non-negative numbers, at least one of them positive, that sets the probability of each element. It must have the same size as data along dimension dim. Elements with an Inf weight are sampled before any other: always when sampling with replacement, first when sampling without. Elements sharing an Inf weight are equally likely, whereas MATLAB draws only the first of them when sampling with replacement.

See also: rand, randi, randperm, randsample

Source Code: datasample