sprandsym

Sparse symmetric random matrix.

Category: Mathematics > Sparse Matrices

Syntax

R = sprandsym(n, density)

Returns an n-by-n symmetric sparse matrix with normally distributed entries and about density*n^2 nonzeros.

R = sprandsym(S)

Returns a symmetric matrix with the pattern of the lower triangle of S, mirrored.

Description

Random entries are drawn for one triangle and mirrored across the diagonal, so R == R'. The matrix is usually not positive definite; to test pcg or ichol add a multiple of the identity large enough to make the diagonal dominant, for example R + n*speye(n).

Input arguments

n
Order of the matrix.
density
Fraction of entries that are nonzero.

Output arguments

R
Symmetric sparse matrix, generally indefinite.

Example

A symmetric random matrix

rng(1);
R = sprandsym(5, 0.4);
isequal(R, R')
issparse(R)
Output
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 sprandn issymmetric speye