/usr/share/octave/packages/statistics-1.2.4/randsample.m is in octave-statistics 1.2.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | ## Copyright (C) 2014 - Nir Krakauer
##
## This progrm is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## Author: Nir Krakauer <nkrakauer@ccny.cuny.com>
## -*- texinfo -*-
## @deftypefn {Function File} {@var{y} =} randsample (@var{v}, @var{k}, @var{replacement}=false [, @var{w}])
## Elements sampled from a vector.
##
## Returns @var{k} random elements from a vector @var{v} with @var{n} elements, sampled without or with @var{replacement}.
##
## If @var{v} is a scalar, samples from 1:@var{v}.
##
## If sampling with replacement, can specify a weight vector @var{w} of the same size as @var{v} such that the probablility of each element being sampled is proportional to @var{w}.
##
## Randomization is performed using rand().
##
## @seealso{randperm}
## @end deftypefn
function y = randsample(v,k,replacement=false,w=[])
if isscalar (v)
n = v;
vector_v = false;
elseif isvector (v)
n = numel (v);
vector_v = true;
else
error ('Octave:invalid-input-arg', 'The input v must be a vector or positive integer.');
endif
if k < 0 || k > n
error ('Octave:invalid-input-arg', 'The input k must be an integer between 0 and n.');
endif
if replacement #sample with replacement
if isempty (w) #all elements are equally likely to be sampled
y = round (n * rand(1, k) + 0.5);
else
w = w / sum(w);
w = [0 cumsum(w(:))'];
y = arrayfun(@(x) find(w <= x, 1, "last"), rand (1, k)); #distribute k uniform random deviates based on the given weighting
endif
else #sample without replacement
y = randperm (n, k);
endif
if vector_v
y = v(y);
endif
endfunction
%!test
%! n = 20;
%! k = 5;
%! x = randsample(n, k);
%! assert (size(x), [1 k]);
%! x = randsample(n, k, true);
%! assert (size(x), [1 k]);
%! x = randsample(n, k, false);
%! assert (size(x), [1 k]);
%! x = randsample(n, k, true, ones(n, 1));
%! assert (size(x), [1 k]);
%! x = randsample(1:n, k);
%! assert (size(x), [1 k]);
%! x = randsample(1:n, k, true);
%! assert (size(x), [1 k]);
%! x = randsample(1:n, k, false);
%! assert (size(x), [1 k]);
%! x = randsample(1:n, k, true, ones(n, 1));
%! assert (size(x), [1 k]);
%! x = randsample((1:n)', k);
%! assert (size(x), [k 1]);
%! x = randsample((1:n)', k, true);
%! assert (size(x), [k 1]);
%! x = randsample((1:n)', k, false);
%! assert (size(x), [k 1]);
%! x = randsample((1:n)', k, true, ones(n, 1));
%! assert (size(x), [k 1]);
|