7017
|
1 ## Copyright (C) 2004, 2005, 2006, 2007 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) |
|
50 [i, j, v, nr, nc] = spfind (m); |
|
51 S = sparse (i, j, rand (size (v)), nr, nc); |
|
52 elseif (nargin == 3) |
5164
|
53 mn = n*m; |
6498
|
54 ## how many entries in S would be satisfactory? |
|
55 k = round (d*mn); |
|
56 idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1; |
|
57 ## idx contains random numbers in [1,mn] |
|
58 ## generate 1% or 10 more random values than necessary in order to |
|
59 ## reduce the probability that there are less than k distinct |
|
60 ## values; maybe a better strategy could be used but I don't think |
|
61 ## it's worth the price |
|
62 |
|
63 ## actual number of entries in S |
|
64 k = min (length (idx), k); |
|
65 j = floor ((idx(1:k)-1)/m); |
5164
|
66 i = idx(1:k) - j*m; |
6498
|
67 if (isempty (i)) |
|
68 S = sparse (m, n); |
5164
|
69 else |
6498
|
70 S = sparse (i, j+1, rand (k, 1), m, n); |
5164
|
71 endif |
|
72 else |
6046
|
73 print_usage (); |
5164
|
74 endif |
|
75 endfunction |