spalloc - Allocate space for sparse matrix - MATLAB (original) (raw)
Allocate space for sparse matrix
Syntax
Description
`S` = spalloc([m](#mw%5F7d46c77a-953c-4565-8c3d-c16b71e936da),[n](#mw%5Fa66f1fe6-4939-4a29-a4f1-3c8636ce2a42),[nz](#mw%5F7fff94d1-afe2-4ffd-a8a1-041f77b19036))
creates an all-zero sparse matrix S
of sizem
-by-n
with room to hold nz
nonzero elements, where nz >= 1
.
Examples
Create Sparse Matrix with Specified Size and Preallocation
Use spalloc
to initialize a 10-by-10 all-zero sparse matrix with room for up to 20 nonzero elements.
Define several elements in the matrix.
S = 10x10 sparse double matrix (9 nonzeros) (1,1) 8 (2,1) 3 (3,1) 4 (1,2) 1 (2,2) 5 (3,2) 9 (1,3) 6 (2,3) 7 (3,3) 2
Show the number of nonzero elements in the matrix.
Show the amount of storage allocated for nonzero matrix elements.
Create Sparse Matrix with Nonzero Columns
Use spalloc
to initialize a 20-by-20 all-zero sparse matrix with space for 100 nonzero elements.
n = 20; S = spalloc(n,n,5*n);
Then use a for
loop to fill in the columns of S
one at a time with an average of at most five nonzero elements per column.
for j = 1:n S(:,j) = [zeros(n-5,1); round(rand(5,1))]; end
Plot the sparsity pattern of matrix S
. The dots represent the nonzero elements.
Show the number of nonzero elements in the matrix.
Show the amount of storage allocated for nonzero matrix elements.
Input Arguments
m
— Number of matrix rows
nonnegative integer
Number of matrix rows, specified as a nonnegative integer.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
n
— Number of matrix columns
nonnegative integer
Number of matrix columns, specified as a nonnegative integer.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
nz
— Storage allocation for nonzero elements
nonnegative integer
Storage allocation for nonzero elements, specified as a nonnegative integer. If you specify a value of 0 for nz
, then spalloc
instead sets the value of nz
to 1.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Limitations
- Both matrix dimensions,
m
andn
, must be smaller than2^31-1
on 32-bit platforms, or2^48-1
on 64-bit platforms.
Tips
- When you assign several times into a matrix you created with
spalloc
, the preallocated memory can prevent repeated reallocations. However, assigning into a sparse matrix is still a relatively expensive operation, which should usually be avoided if it can easily be replaced by one of the following:
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
- See distributed.spalloc (Parallel Computing Toolbox).
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a