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} = } randerr (@var{n})
18 ## @deftypefnx {Function File} {@var{b} = } randerr (@var{n},@var{m})
19 ## @deftypefnx {Function File} {@var{b} = } randerr (@var{n},@var{m},@var{err})
20 ## @deftypefnx {Function File} {@var{b} = } randerr (@var{n},@var{m},@var{err},@var{seed})
22 ## Generate a matrix of random bit errors. The size of the matrix is
23 ## @var{n} rows by @var{m} columns. By default @var{m} is equal to @var{n}.
24 ## Bit errors in the matrix are indicated by a 1.
26 ## The variable @var{err} determines the number of errors per row. By
27 ## default the return matrix @var{b} has exactly one bit error per row.
28 ## If @var{err} is a scalar, there each row of @var{b} has exactly this
29 ## number of errors per row. If @var{err} is a vector then each row has
30 ## a number of errors that is in this vector. Each number of errors has
31 ## an equal probability. If @var{err} is a matrix with two rows, then
32 ## the first row determines the number of errors and the second their
35 ## The variable @var{seed} allows the random number generator to be seeded
36 ## with a fixed value. The initial seed will be restored when returning.
42 function b = randerr (n, m, err, seed)
61 usage ("b = randerr (n, [m, [err, [seed]]])");
70 elseif ((ac > 1) && (ar != 1) && (ar != 2))
71 error ("randerr: err must be a scalar, vector or two row matrix");
75 error ("randerr: illegal number of errors per row");
79 # Use randsrc to calculate the number of errors per row
80 nerrs = randsrc (n, 1, err, seed);
82 # Now put to errors into place in the return matrix
87 indx = sort(randint(1,nerrs(i),m,seed));
90 indx = sort(randint(1,nerrs(i),m,seed));
91 until (! any(indx(1:nerrs(i)-1) == indx(2:nerrs(i))))
93 b(i,indx+1) = ones(1,nerrs(i));
99 %!shared n, err1, err2, seed, a1, a2, a3, a4, a5, a6
100 %! n = 10; err1 = 2; err2 = [1,2;0.7,0.3] ; seed = 1;
101 %! a1 = randerr(n); a2 = randerr(n,n);
102 %! a3 = randerr(n,n,err1); a4 = randerr(n,n,err2);
103 %! a5 = randerr(n,n,err1,seed); a6 = randerr(n,n,err1,seed);
105 %!error randerr (n,n,n,n,n);
106 %!assert (size(a1) == [n, n] && size(a2) == [n, n]);
107 %!assert (all (sum (a1.') == 1) && all (sum (a2.') == 1))
108 %!assert (all((a1(:) == 1 | a1(:) == 0)) &&all((a2(:) == 1 | a2(:) == 0)))
109 %!assert (size(a3) == [n, n] && size(a4) == [n, n]);
110 %!assert (all (sum (a3.') == err1))
111 %!assert (all((a3(:) == 1 | a3(:) == 0)))
112 %!assert (all ((sum (a4.') == err2(1,1)) | (sum (a4.') == err2(1,2))))
113 %!assert (all((a4(:) == 1 | a4(:) == 0)))
114 %!assert (a5(:) == a6(:));