Mercurial > hg > octave-nkf
diff scripts/sparse/sprand.m @ 13196:5976ba269538
Simplify code in sprand and use two-arg form of randperm for precise density
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Wed, 21 Sep 2011 21:37:12 -0500 |
parents | 14422cc782b2 |
children | 6db186dfdeaa |
line wrap: on
line diff
--- a/scripts/sparse/sprand.m +++ b/scripts/sparse/sprand.m @@ -73,30 +73,17 @@ mn = m*n; ## how many entries in S would be satisfactory? k = round (d*mn); - idx = unique (fix (rand (min (k*1.01, k+10), 1) * mn)) + 1; - ## idx contains random numbers in [1,mn] - ## generate 1% or 10 more random values than necessary in order to - ## reduce the probability that there are less than k distinct - ## values; maybe a better strategy could be used but I don't think - ## it's worth the price + idx = randperm (mn, k); - ## actual number of entries in S - k = min (length (idx), k); - j = floor ((idx(1:k)-1)/m); - i = idx(1:k) - j*m; - if (isempty (i)) - S = sparse (m, n); - else - S = sparse (i, j+1, rand (k, 1), m, n); - endif + [i, j] = ind2sub ([m, n], idx); + S = sparse (i, j, rand (k, 1), m, n); endfunction -## FIXME: Test for density can't happen until code of sprand is improved %!test %! s = sprand (4, 10, 0.1); %! assert (size (s), [4, 10]); -##%! assert (nnz (s) / numel (s), 0.1, .01); +%! assert (nnz (s) / numel (s), 0.1); %% Test 1-input calling form %!test