# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1316659032 18000 # Node ID 5976ba269538e94d1163de486fe7e90af3173b42 # Parent 08650b6fbf675391b954dc427342848c6d8a4d75 Simplify code in sprand and use two-arg form of randperm for precise density diff --git a/scripts/sparse/sprand.m b/scripts/sparse/sprand.m --- 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