Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - Português


sample

Sampling: random draw with replacement

Syntax

s = sample(n, X, orient)

Arguments

n

positive integer (size of sample).

X

array of any data type, or cells array. Samples will be extracted from this array.

orient

The shape of samples:

  • '*' (default): elements of X are sampled.
  • 1 or 'r': rows of X are sampled.
  • 2 or 'c': rolumns of X are sampled.

s

vector, matrix, or cells array containing the samples, of same type as X.

Description

s = sample(n, X) (or s = sample(n, X, '*')) returns a vector s of n elements randomly chosen among X elements, with replacement.

s = sample(n, X, 'r') (or s = sample(n, X, 1)) returns a matrix of size n x size(X,'c') of randomly chosen Rows of X, with replacement.

s = sample(n, X, 'c') (or s = sample(n, X, 2)) returns a matrix of size size(X,'r') x n of randomly chosen Columns of X, with replacement.

Random number generator

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

seed = getdate("s");
grand("setsd", seed); //sets the seed to current date

seed = 0;
grand("setsd", seed); //sets the seed to default value

Examples

Sampling some numbers:

data = (1:9)' * 10.^(-3:3)
sample(8, data)
sample(5, data, "r")
sample(5, data, "c")
--> data = (1:9)' * 10.^(-3:3)
 data  =
   0.001   0.01   0.1   1.   10.   100.   1000.
   0.002   0.02   0.2   2.   20.   200.   2000.
   0.003   0.03   0.3   3.   30.   300.   3000.
   0.004   0.04   0.4   4.   40.   400.   4000.
   0.005   0.05   0.5   5.   50.   500.   5000.
   0.006   0.06   0.6   6.   60.   600.   6000.
   0.007   0.07   0.7   7.   70.   700.   7000.
   0.008   0.08   0.8   8.   80.   800.   8000.
   0.009   0.09   0.9   9.   90.   900.   9000.

--> sample(8, data)
 ans  =
   0.02   0.06   10.   0.009   0.007   70.   6000.   60.

--> sample(5, data, "r")
 ans  =
   0.002   0.02   0.2   2.   20.   200.   2000.
   0.009   0.09   0.9   9.   90.   900.   9000.
   0.005   0.05   0.5   5.   50.   500.   5000.
   0.002   0.02   0.2   2.   20.   200.   2000.
   0.004   0.04   0.4   4.   40.   400.   4000.

--> sample(5, data, "c")
 ans  =
   10.   0.1   100.   0.1   0.001
   20.   0.2   200.   0.2   0.002
   30.   0.3   300.   0.3   0.003
   40.   0.4   400.   0.4   0.004
   50.   0.5   500.   0.5   0.005
   60.   0.6   600.   0.6   0.006
   70.   0.7   700.   0.7   0.007
   80.   0.8   800.   0.8   0.008
   90.   0.9   900.   0.9   0.009

Sampling some texts:

data = [
  "a"  "aa"  "aaa"  "aaaa"  "aaaaa"  "aaaaaa"
  "b"  "bb"  "bbb"  "bbbb"  "bbbbb"  "bbbbbb"
  "c"  "cc"  "ccc"  "cccc"  "ccccc"  "cccccc"
  "d"  "dd"  "ddd"  "dddd"  "ddddd"  "dddddd"
  "e"  "ee"  "eee"  "eeee"  "eeeee"  "eeeeee"
  "f"  "ff"  "fff"  "ffff"  "fffff"  "ffffff" ];

sample(5, data)
sample(4, data, "r")
sample(4, data, "c")
--> sample(5, data)
 ans  =
  "ffff"  "bb"  "aaaa"  "d"  "c"

--> sample(4, data, "r")
 ans  =
  "d"  "dd"  "ddd"  "dddd"  "ddddd"  "dddddd"
  "f"  "ff"  "fff"  "ffff"  "fffff"  "ffffff"
  "d"  "dd"  "ddd"  "dddd"  "ddddd"  "dddddd"
  "b"  "bb"  "bbb"  "bbbb"  "bbbbb"  "bbbbbb"

--> sample(4, data, "c")
 ans  =
  "aaaaaa"  "a"  "aaa"  "a"
  "bbbbbb"  "b"  "bbb"  "b"
  "cccccc"  "c"  "ccc"  "c"
  "dddddd"  "d"  "ddd"  "d"
  "eeeeee"  "e"  "eee"  "e"
  "ffffff"  "f"  "fff"  "f"

Sampling some cells:

data = {%f, %pi, "abc", 1-%z ; 1+%z, "ABC", %e, %f}
sample(5, data)
sample(3, data, "r")
sample(3, data, "c")
--> data = {%f, %pi, "abc", 1-%z ; 1+%z, "ABC", %e, %f}
 data  =
  [1x1 boolean   ]  [1x1 constant]  [1x1 string  ]  [1x1 polynomial]
  [1x1 polynomial]  [1x1 string  ]  [1x1 constant]  [1x1 boolean   ]

--> sample(5, data)
 ans  =
  [1x1 constant]  [1x1 polynomial]  [1x1 string]  [1x1 string]  [1x1 boolean]

--> sample(3, data, "r")
 ans  =
  [1x1 boolean   ]  [1x1 constant]  [1x1 string  ]  [1x1 polynomial]
  [1x1 polynomial]  [1x1 string  ]  [1x1 constant]  [1x1 boolean   ]
  [1x1 polynomial]  [1x1 string  ]  [1x1 constant]  [1x1 boolean   ]

--> sample(3, data, "c")
 ans  =
  [1x1 polynomial]  [1x1 boolean   ]  [1x1 string  ]
  [1x1 boolean   ]  [1x1 polynomial]  [1x1 constant]

See also

  • samplef — sample values with replacement from a population with given frequencies.
  • samwr — Sampling without replacement
  • grand — Generate random numbers
Report an issue
<< rand random samplef >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jun 17 17:53:23 CEST 2024