Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.

See the recommended documentation of this function

# samplef

sample values with replacement from a population with given frequencies.

### Syntax

s = samplef(n,X,f,orient)

### Arguments

- n
positive integer (size of sample)

- X
matrix. Population, samples are extracted from this matrix

- f
matrix of positive real values. Indicates the corresponding frequencies of the values in

`X`

.- orient
Optional parameter. Admissible values are

`{'*'}, 1, 2, 'r'`

or`'c'`

. The orientation to sample rows or columns. See Description below.- s
vector or matrix containing the sampled values of

`X`

at frequencies`f`

.

### Description

This function returns `s`

, a vector of length `n`

. It contains
a sample of `n`

extractions, with replacement, from the
vector (or matrix) `X`

, each element counted with the
frequency given by the corresponding value in vector `f`

.

`s=samplef(n,X,f)`

(or
`s=samplef(n,X,f,'*')`

) returns a
vector `s`

whose values are a random sample of n values from
`X`

, each value with a probability to be sampled
proportional to the corresponding value of `f`

, extracted
with replacement, from `X`

. `f`

must have same length than `X`

.

`s=samplef(n,X,f,'r')`

(or, equivalently,
`s=samplef(n,X,f,1))`

returns a matrix of type
`size(X,'r') * n`

. It contains a random sample of `n`

rows from
`X`

, each row with a probability to be sampled proportional
to the corresponding value of `f`

, extracted with
replacement, from the rows of `X`

. The length of `f`

must be
equal to the number of rows of `X`

.

`s=samplef(n,X,f,'c')`

(or, equivalently,
`s=samplef(n,X,f,2))`

returns a matrix of type
`n * size(X,'c')`

. It contains a random sample of `n`

columns
from `X`

, each column with a probability to be sampled
proportional to the corresponding value of `f`

, extracted
with replacement, from the columns of `X`

. The length of `f`

must be equal to the number of columns of `X`

.

### Random number generator

`samplef`

is based on grand for generating the random samples. Use `grand("setsd", seed)`

to change the seed for `samplef`

.

### Examples

X = [3 7 9; 22 4 2]; f1 = [10 1 1 1 1 1]; f2 = [1 ; 15]; f3 = [10 1 1]; s=samplef(20, X, f1) //the first value of matrix X will be chosen with frequency 10/15 s=samplef(20, X, f2, 'r') //the second row of matrix a will be chosen with frequency 15/16 s=samplef(20, X, f3, 'c') //the first column of matrix a will be chosen with frequency 10/12

### See also

Report an issue | ||

<< sample | random | samwr >> |