## octave-cvsupdate

 [Octave-cvsupdate] octave-forge/main/sparse sprand.m,1.2,1.3 From: przykry2004 - 2004-09-27 15:26:26 ```Update of /cvsroot/octave/octave-forge/main/sparse In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29263 Modified Files: sprand.m Log Message: Used Paul's hint to allow larger random matrices, at the price of sometimes lower density than desired Index: sprand.m =================================================================== RCS file: /cvsroot/octave/octave-forge/main/sparse/sprand.m,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- sprand.m 27 Nov 2002 08:40:11 -0000 1.2 +++ sprand.m 27 Sep 2004 15:26:16 -0000 1.3 @@ -3,8 +3,8 @@ ## Generate a random sparse matrix. m and n give the dimensions, d ## gives the density (between 0 and 1). ## -## Note: this will fail for large m x n matrices, even if they -## are very sparse. +## Note: sometimes the actual density of S may be a bit smaller than d. This is +## unlikely to happen for large really sparse matrices. ## ## S = sprand(Q) ## @@ -13,18 +13,27 @@ ## This program is public domain ## Author: Paul Kienzle +## +## Changelog: +## +## Piotr Krzyzanowski +## 2004-09-27 use Paul's hint to allow larger random matrices +## at the price of sometimes lower density than desired function S = sprand(m,n,d) if nargin == 1 [i,j,v,nr,nc] = spfind(m); S = sparse(i,j,rand(size(v)),nr,nc); elseif nargin == 3 - k = round(d*m*n); - ## XXX FIXME XXX need a better algorithm for large sparse matrices - ## E.g., find p such that p samples out of m*n things with replacement - ## yields on average k unique values, and use - ## idx=unique(fix(rand(1,p)*m*n))+1 - [v,idx] = sort(rand(m*n,1)); + mn = n*m; + k = round(d*mn); # how many entries in S would be satisfactory? + idx=unique(fix(rand(k+10,1)*mn))+1; # idx contains random numbers in [1,mn] + # generate 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 + k = min(length(idx),k); # actual number of entries in S j = floor((idx(1:k)-1)/m); i = idx(1:k) - j*m; if isempty(i) ```