[d6dad2]: inst / randsrc.m Maximize Restore History

Download this file

randsrc.m    118 lines (104 with data), 3.8 kB

  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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
## Copyright (C) 2003 David Bateman
##
## This program 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/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{b} =} randsrc (@var{n})
## @deftypefnx {Function File} {@var{b} =} randsrc (@var{n}, @var{m})
## @deftypefnx {Function File} {@var{b} =} randsrc (@var{n}, @var{m}, @var{alphabet})
## @deftypefnx {Function File} {@var{b} =} randsrc (@var{n}, @var{m}, @var{alphabet}, @var{seed})
##
## Generate a matrix of random symbols. The size of the matrix is
## @var{n} rows by @var{m} columns. By default @var{m} is equal to @var{n}.
##
## The variable @var{alphabet} can be either a row vector or a matrix with
## two rows. When @var{alphabet} is a row vector the symbols returned in
## @var{b} are chosen with equal probability from @var{alphabet}. When
## @var{alphabet} has two rows, the second row determines the probability
## with which each of the symbols is chosen. The sum of the probabilities
## must equal 1. By default @var{alphabet} is [-1 1].
##
## The variable @var{seed} allows the random number generator to be seeded
## with a fixed value. The initial seed will be restored when returning.
## @end deftypefn
function b = randsrc (n, m, alphabet, seed)
switch (nargin)
case 0
m = 1;
n = 1;
alphabet = [-1, 1];
seed = Inf;
case 1
m = n;
alphabet = [-1, 1];
seed = Inf;
case 2
alphabet = [-1, 1];
seed = Inf;
case 3
seed = Inf;
case 4
otherwise
print_usage ();
endswitch
## Check alphabet
[ar, ac] = size (alphabet);
if (ac == 1)
b = alphabet (1, 1) * ones (n, m);
return;
endif
if (ar == 1)
prob = [1:ac] / ac;
elseif (ar == 2)
prob = alphabet(2,:);
alphabet = alphabet(1,:);
if (abs (1 - sum (prob)) > eps)
error ("randsrc: probabilities must added up to one");
endif
prob = cumsum (prob);
else
error ("randsrc: alphabet must have 1 or 2 rows");
endif
## Check seed;
if (!isinf (seed))
old_seed = rand ("seed");
rand ("seed", seed);
endif
## Create indexes with the right probabilities
tmp = rand (n, m);
b = ones (n, m);
for i = 1:ac-1
b = b + (tmp > prob(i));
endfor
## Map the indexes to the symbols
b = alphabet(b);
## BUG: the above gives a row vector for some reason. Force what we want
b = reshape (b, n, m);
## Get back to the old
if (!isinf (seed))
rand ("seed", old_seed);
endif
endfunction
%!shared n, alph1, alph2, seed, a1, a2, a3, a4, a5, a6
%! n = 10; alph1 = [0, 1; 0.3, 0.7]; alph2 = ["a", "b"]; seed = 1;
%! a1 = randsrc (n); a2 = randsrc (n, n);
%! a3 = randsrc (n, n, alph1); a4 = randsrc (n, n, alph2);
%! a5 = randsrc (n, n, alph1, seed); a6 = randsrc (n, n, alph1, seed);
%!error randsrc (n, n, n, n, n);
%!assert (size (a1) == [n, n] && size (a2) == [n, n]);
%!assert (max ([a1(:); a2(:)]) <= 1 && min ([a1(:); a2(:)]) >= -1);
%!assert (size (a3) == [n, n] && size (a4) == [n, n]);
%!assert (max (a3(:)) <= 1 && min (a3(:)) >= 0);
%!assert (max (toascii (a4(:))) <= toascii ("b"))
%!assert (max (toascii (a4(:))) >= toascii ("a"))
%!assert (a5(:) == a6(:));