Mercurial > hg > octave-nkf
annotate scripts/sparse/sprandn.m @ 10793:be55736a0783
Grammarcheck the documentation from m-files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 18 Jul 2010 20:35:16 -0700 |
parents | 16f53d29049f |
children | fd0a3ac60b0e |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Paul Kienzle |
5164 | 2 ## |
7016 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 ## | |
19 ## Original version by Paul Kienzle distributed as free software in the | |
20 ## public domain. | |
5164 | 21 |
22 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
23 ## @deftypefn {Function File} {} sprandn (@var{m}, @var{n}, @var{d}) |
5610 | 24 ## @deftypefnx {Function File} {} sprandn (@var{s}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## Generate a random sparse matrix. The size of the matrix will be |
5164 | 26 ## @var{m} by @var{n}, with a density of values given by @var{d}. |
27 ## @var{d} should be between 0 and 1. Values will be normally | |
28 ## distributed with mean of zero and variance 1. | |
29 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## Note: sometimes the actual density may be a bit smaller than @var{d}. |
5164 | 31 ## This is unlikely to happen for large really sparse matrices. |
32 ## | |
33 ## If called with a single matrix argument, a random sparse matrix is | |
34 ## generated wherever the matrix @var{S} is non-zero. | |
5642 | 35 ## @seealso{sprand} |
5164 | 36 ## @end deftypefn |
37 | |
38 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
39 | |
6498 | 40 function S = sprandn (m, n, d) |
41 if (nargin == 1) | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
42 [i, j, v] = find (m); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
43 [nr, nc] = size (m); |
6498 | 44 S = sparse (i, j, randn (size (v)), nr, nc); |
45 elseif (nargin == 3) | |
5164 | 46 mn = m*n; |
6498 | 47 k = round (d*mn); |
48 idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1; | |
49 ## idx contains random numbers in [1,mn] | |
50 ## generate 1% or 10 more random values than necessary in order to | |
51 ## reduce the probability that there are less than k distinct | |
52 ## values; maybe a better strategy could be used but I don't think | |
53 ## it's worth the price. | |
54 | |
55 ## actual number of entries in S | |
56 k = min (length (idx), k); | |
57 j = floor ((idx(1:k)-1)/m); | |
5164 | 58 i = idx(1:k) - j*m; |
6498 | 59 if (isempty (i)) |
60 S = sparse (m, n); | |
5164 | 61 else |
6498 | 62 S = sparse (i, j+1, randn (k, 1), m, n); |
5164 | 63 endif |
64 else | |
6046 | 65 print_usage (); |
5164 | 66 endif |
67 endfunction |