How to create a sparse matrix obeying (-1,1) uniform distribution

question
#1

I try to code

using SparseArrays
using Distributions
sprandn(100,100,0.3,Uniform(-1,1))

But it failed. So how to generate a sparse matrix obeying (-1,1) uniform distribution? Thanks

0 Likes

#2

You need sprand rather than sprandn, and the last argument should be a function that maps n to a vector of n samples (though the docs don’t seem to say so anywhere).

sprand(100,100,0.1,n->rand(Uniform(-1,1),n))
0 Likes