Mercurial > hg > octave-lyh
comparison scripts/sparse/sprand.m @ 6498:2c85044aa63f
[project @ 2007-04-05 17:59:47 by jwe]
author | jwe |
---|---|
date | Thu, 05 Apr 2007 17:59:47 +0000 |
parents | 34f96dd5441b |
children | 93c65f2a5668 |
comparison
equal
deleted
inserted
replaced
6497:fc8ed0c77e08 | 6498:2c85044aa63f |
---|---|
28 ## at the price of sometimes lower density than desired | 28 ## at the price of sometimes lower density than desired |
29 ## David Bateman | 29 ## David Bateman |
30 ## 2004-10-20 Texinfo help and copyright message | 30 ## 2004-10-20 Texinfo help and copyright message |
31 | 31 |
32 function S = sprand (m, n, d) | 32 function S = sprand (m, n, d) |
33 if nargin == 1 | 33 if (nargin == 1) |
34 [i,j,v,nr,nc] = spfind(m); | 34 [i, j, v, nr, nc] = spfind (m); |
35 S = sparse (i,j,rand(size(v)),nr,nc); | 35 S = sparse (i, j, rand (size (v)), nr, nc); |
36 elseif nargin == 3 | 36 elseif (nargin == 3) |
37 mn = n*m; | 37 mn = n*m; |
38 k = round(d*mn); # how many entries in S would be satisfactory? | 38 ## how many entries in S would be satisfactory? |
39 idx=unique(fix(rand(min(k*1.01,k+10),1)*mn))+1; | 39 k = round (d*mn); |
40 # idx contains random numbers in [1,mn] | 40 idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1; |
41 # generate 1% or 10 more random values than necessary | 41 ## idx contains random numbers in [1,mn] |
42 # in order to reduce the probability that there are less than k | 42 ## generate 1% or 10 more random values than necessary in order to |
43 # distinct values; | 43 ## reduce the probability that there are less than k distinct |
44 # maybe a better strategy could be used | 44 ## values; maybe a better strategy could be used but I don't think |
45 # but I don't think it's worth the price | 45 ## it's worth the price |
46 k = min(length(idx),k); # actual number of entries in S | 46 |
47 j = floor((idx(1:k)-1)/m); | 47 ## actual number of entries in S |
48 k = min (length (idx), k); | |
49 j = floor ((idx(1:k)-1)/m); | |
48 i = idx(1:k) - j*m; | 50 i = idx(1:k) - j*m; |
49 if isempty(i) | 51 if (isempty (i)) |
50 S = sparse(m,n); | 52 S = sparse (m, n); |
51 else | 53 else |
52 S = sparse(i,j+1,rand(k,1),m,n); | 54 S = sparse (i, j+1, rand (k, 1), m, n); |
53 endif | 55 endif |
54 else | 56 else |
55 print_usage (); | 57 print_usage (); |
56 endif | 58 endif |
57 endfunction | 59 endfunction |