sprandn

Sparse matrix with normally distributed random entries.

Category: Mathematics > Sparse Matrices

Syntax

R = sprandn(m, n, density)

Returns an m-by-n sparse matrix with about density*m*n nonzeros.

R = sprandn(S)

Returns a random matrix with the same sparsity pattern as S.

Description

It works like sprand, except that the values are drawn from the normal distribution with mean 0 and variance 1, so they can be negative. Seed with rng to repeat a result.

Input arguments

m, n
Size of the matrix.
density
Fraction of entries that are nonzero.
S
Sparse matrix whose pattern is copied.

Output arguments

R
Sparse matrix whose nonzeros come from the standard normal distribution.

Example

A random matrix with negative entries

rng(1);
R = sprandn(5, 5, 0.4);
size(R)
issparse(R)
any(nonzeros(R) < 0)
Output
ans = 1×2

     5     5
ans = logical
   1
ans = logical
   1

Notes

The matrices produced for a given seed differ from MATLAB's. MatLivre stores sparse matrices densely, so they save no memory, and sizes above 200 million elements are rejected. Results and display match MATLAB for ordinary sizes.

See also

sprand sprandsym randn sparse