Mercurial > hg > octave-lyh
annotate scripts/sparse/sprand.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | f5005d9510f4 |
children | 1bf0ce0930be |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2004, 2005, 2006, 2007, 2008 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 -*- | |
23 ## @deftypefn {Function File} {} sprand (@var{m}, @var{n}, @var{d}) | |
24 ## @deftypefnx {Function File} {} sprand (@var{s}) | |
25 ## Generate a random sparse matrix. The size of the matrix will be | |
26 ## @var{m} by @var{n}, with a density of values given by @var{d}. | |
5610 | 27 ## @var{d} should be between 0 and 1. Values will be uniformly |
28 ## distributed between 0 and 1. | |
5164 | 29 ## |
30 ## Note: sometimes the actual density may be a bit smaller than @var{d}. | |
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{sprandn} |
5164 | 36 ## @end deftypefn |
37 | |
38 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
39 ## | |
40 ## Changelog: | |
41 ## | |
42 ## Piotr Krzyzanowski <przykry2004@users.sf.net> | |
43 ## 2004-09-27 use Paul's hint to allow larger random matrices | |
44 ## at the price of sometimes lower density than desired | |
45 ## David Bateman | |
46 ## 2004-10-20 Texinfo help and copyright message | |
47 | |
48 function S = sprand (m, n, d) | |
6498 | 49 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
|
50 [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
|
51 [nr, nc] = size (m); |
6498 | 52 S = sparse (i, j, rand (size (v)), nr, nc); |
53 elseif (nargin == 3) | |
5164 | 54 mn = n*m; |
6498 | 55 ## how many entries in S would be satisfactory? |
56 k = round (d*mn); | |
57 idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1; | |
58 ## idx contains random numbers in [1,mn] | |
59 ## generate 1% or 10 more random values than necessary in order to | |
60 ## reduce the probability that there are less than k distinct | |
61 ## values; maybe a better strategy could be used but I don't think | |
62 ## it's worth the price | |
63 | |
64 ## actual number of entries in S | |
65 k = min (length (idx), k); | |
66 j = floor ((idx(1:k)-1)/m); | |
5164 | 67 i = idx(1:k) - j*m; |
6498 | 68 if (isempty (i)) |
69 S = sparse (m, n); | |
5164 | 70 else |
6498 | 71 S = sparse (i, j+1, rand (k, 1), m, n); |
5164 | 72 endif |
73 else | |
6046 | 74 print_usage (); |
5164 | 75 endif |
76 endfunction |