home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------//
-
- // Synopsis: Random matrix with elements -1, 0 or 1.
-
- // Syntax: A = rando ( N , K )
-
- // Description:
-
- // A is a random N-by-N matrix with elements from one of the
- // following discrete distributions (default K = 1):
-
- // K = 1: A(i,j) = 0 or 1 with equal probability,
- // K = 2: A(i,j) = -1 or 1 with equal probability,
- // K = 3: A(i,j) = -1, 0 or 1 with equal probability.
-
- // N may be a 2-vector, in which case the matrix is N(1)-by-N(2).
-
- // This file is a translation of rando.m from version 2.0 of
- // "The Test Matrix Toolbox for Matlab", described in Numerical
- // Analysis Report No. 237, December 1993, by N. J. Higham.
-
- //-------------------------------------------------------------------//
-
- rando = function ( n, k )
- {
- local (n, k)
-
- if (!exist (k)) { k = 1; }
-
- m = n[1]; // Parameter n specifies dimension: m-by-n.
-
- n = n[max(size(n))];
-
- rand("uniform", 0, 1);
- if (k == 1) // {0, 1}
- {
- A = floor( rand(m,n) + .5 );
- else if (k == 2) { // {-1, 1}
- A = 2*floor( rand(m,n) + .5 ) - 1;
- else if (k == 3) { // {-1, 0, 1}
- A = round( 3*rand(m,n) - 1.5 );
- }}}
-
- return A;
- };
-