sprand - Sparse uniformly distributed random matrix - MATLAB (original) (raw)
Sparse uniformly distributed random matrix
Syntax
Description
[R](#mw%5F1ee8e6d3-8c8a-451b-8570-f5ca88758a6f) = sprand([S](#mw%5F197371c0-ec2a-4ecc-83c8-3920acb77621))
creates a sparse matrix that has the same sparsity pattern as the matrix S
, but with uniformly distributed random entries.
[R](#mw%5F1ee8e6d3-8c8a-451b-8570-f5ca88758a6f) = sprand([m](#mw%5F04f3068e-1b3b-4987-8ea0-b02812f31acf),[n](#mw%5Fc8d6db30-40e2-40d2-bf19-f6fa3d2e2147),[density](#mw%5Fd7db3873-a38e-40f9-b67a-e8d6dfbf2d18))
creates a random m
-by-n
sparse matrix with approximately density*m*n
uniformly distributed nonzero entries fordensity
in the interval [0,1].
[R](#mw%5F1ee8e6d3-8c8a-451b-8570-f5ca88758a6f) = sprand([m](#mw%5F04f3068e-1b3b-4987-8ea0-b02812f31acf),[n](#mw%5Fc8d6db30-40e2-40d2-bf19-f6fa3d2e2147),[density](#mw%5Fd7db3873-a38e-40f9-b67a-e8d6dfbf2d18),[rc](#mw%5F8c02267a-f294-4a66-a175-514a041936a5))
creates a matrix that also has reciprocal condition number approximately equal torc
. The matrix R
is constructed from a sum of matrices of rank one.
[R](#mw%5F1ee8e6d3-8c8a-451b-8570-f5ca88758a6f) = sprand(___,[typename](#mw%5Fc8c09988-52cc-4fc4-9223-230666e51665))
returns a sparse matrix of the specified data type. Specify the data type in addition to any of the input argument combinations in previous syntaxes. (since R2025a)
Examples
Create the 60
-by-60
sparse adjacency matrix of the connectivity graph of the Buckminster Fuller geodesic dome. Plot the sparsity pattern of the matrix S
.
Create another sparse matrix R
that has the same sparsity pattern as the matrix S
, but with uniformly distributed random entries. Plot the sparsity pattern of R
.
Create a random 500
-by-1000
sparse matrix with density 0.1
. The matrix has approximately 0.1*500*1000 = 50000
uniformly distributed nonzero entries.
R = sprand(500,1000,0.1);
Show the exact number of nonzero elements in the matrix R
.
Create a random 50
-by-100
sparse matrix with approximately 0.2*50*100 = 1000
uniformly distributed nonzero entries. Specify the reciprocal condition number of the matrix to be 0.25
.
R = sprand(50,100,0.2,0.25);
Show that the condition number of the matrix R
is equal to 1/0.25 = 4
.
Input Arguments
Input matrix, specified as a full or sparse matrix.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
Number of matrix rows, specified as a nonnegative integer.
Number of matrix columns, specified as a nonnegative integer.
Density of nonzero elements, specified as a scalar. density
must be in the interval [0,1].
Reciprocal condition number, specified as a scalar or vector. rc
must be in the interval [0,1].
If rc
is a vector of length lr
, wherelr <= min(m,n)
, then R = sprand(m,n,density,rc)
has rc
as its firstlr
singular values and all others are zero. In this case,R
is generated by random plane rotations applied to a diagonal matrix with the given singular values. It has a great deal of topological and algebraic structure.
Since R2025a
Output data type, specified as "double"
or"single"
.
Output Arguments
Output matrix, returned as a sparse uniformly distributed random matrix.
Limitations
sprand
is designed to produce large matrices with small density and will generate significantly fewer nonzero values than requested ifm*n
is small ordensity
is large.
Tips
sprand
uses the same random number generator as the rand, randi, and randn functions. You can control this generator with the rng function.
Extended Capabilities
The sprand
function supports GPU array input with these usage notes and limitations:
- To run this function on a GPU and obtain a
gpuArray
output, use any of these syntaxes:R = sprand(S)
, whereS
is agpuArray
.R = sprand(S,typename)
, whereS
is agpuArray
. (since R2025a)R = gpuArray.sprand(S)
.R = gpuArray.sprand(m,n,density)
.R = gpuArray.sprand(___,typename)
, wheretypename
specifies the data type in addition to the input argument combinations in the two previous syntaxes. (since R2025a)
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
You can specify the output data type by specifying the typename
argument as "double"
or "single"
.
The sprand
and sprandn
functions show improved performance when generating random sparse matrices if the number of nonzero elements in the output is larger than the number of rows.
For example, generating a 10,000-by-10,000 matrix with 10% density of nonzero elements is about 2.5x faster than in the previous release.
function timingSprand n = 1e4; d = 0.1; rng default
tic sprand(n,n,d); toc end
The approximate execution times are:
R2021b: 2.7 s
R2022a: 1.1 s
The code was timed on a Windows® 10, Intel® Xeon® CPU W-2133 @ 3.60 GHz test system by calling thetimingSprand
function.