1 ## Copyright (C) 2003 David Bateman
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{b} = } randsrc (@var{n})
18 ## @deftypefnx {Function File} {@var{b} = } randsrc (@var{n},@var{m})
19 ## @deftypefnx {Function File} {@var{b} = } randsrc (@var{n},@var{m},@var{alphabet})
20 ## @deftypefnx {Function File} {@var{b} = } randsrc (@var{n},@var{m},@var{alphabet},@var{seed})
22 ## Generate a matrix of random symbols. The size of the matrix is
23 ## @var{n} rows by @var{m} columns. By default @var{m} is equal to @var{n}.
25 ## The variable @var{alphabet} can be either a row vector or a matrix with
26 ## two rows. When @var{alphabet} is a row vector the symbols returned in
27 ## @var{b} are chosen with equal probability from @var{alphabet}. When
28 ## @var{alphabet} has two rows, the second row determines the probabilty
29 ## with which each of the symbols is chosen. The sum of the probabilities
30 ## must equal 1. By default @var{alphabet} is [-1 1].
32 ## The variable @var{seed} allows the random number generator to be seeded
33 ## with a fixed value. The initial seed will be restored when returning.
39 function b = randsrc (n, m, alphabet, seed)
58 usage ("b = randsrc (n, [m, [alphabet, [seed]]])");
62 [ar,ac] = size (alphabet);
64 b = alphabet (1, 1) * ones (n, m);
72 alphabet = alphabet(1,:);
73 if (abs(1-sum(prob)) > eps)
74 error ("randsrc: probabilities must added up to one");
78 error ("randsrc: alphabet must have 1 or 2 rows");
83 old_seed = rand ("seed");
87 ## Create indexes with the right probabilities
91 b = b + (tmp > prob(i));
94 ## Map the indexes to the symbols
97 ## BUG: the above gives a row vector for some reason. Force what we want
100 ## Get back to the old
102 rand ("seed", old_seed);
107 %!shared n, alph1, alph2, seed, a1, a2, a3, a4, a5, a6
108 %! n = 10; alph1 = [0,1;0.3,0.7]; alph2 = ['a','b']; seed = 1;
109 %! a1 = randsrc(n); a2 = randsrc(n,n);
110 %! a3 = randsrc(n,n,alph1); a4 = randsrc(n,n,alph2);
111 %! a5 = randsrc(n,n,alph1,seed); a6 = randsrc(n,n,alph1,seed);
113 %!error randsrc (n,n,n,n,n);
114 %!assert (size(a1) == [n, n] && size(a2) == [n, n]);
115 %!assert (max ([a1(:); a2(:)]) <= 1 && min([a1(:); a2(:)]) >= -1);
116 %!assert (size(a3) == [n, n] && size(a4) == [n, n]);
117 %!assert (max (a3(:)) <= 1 && min(a3(:)) >= 0);
118 %!assert (max(toascii(a4(:))) <= toascii('b'))
119 %!assert (max(toascii(a4(:))) >= toascii('a'))
120 %!assert (a5(:) == a6(:));