spalloc - Allocate space for sparse matrix - MATLAB (original) (raw)
Allocate space for sparse matrix
Syntax
Description
[S](#mw%5Fd20503bb-158e-44ac-9d2e-e06c2f8b2cf3) = 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
.
[S](#mw%5Fd20503bb-158e-44ac-9d2e-e06c2f8b2cf3) = spalloc([m](#mw%5F7d46c77a-953c-4565-8c3d-c16b71e936da),[n](#mw%5Fa66f1fe6-4939-4a29-a4f1-3c8636ce2a42),[nz](#mw%5F7fff94d1-afe2-4ffd-a8a1-041f77b19036),[typename](#mw%5F8dc31b04-4361-47dd-aae3-433c6230a347))
returns a sparse matrix of the specified data type. (since R2025a)
Examples
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 = 10×10 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.
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
Number of matrix rows, specified as a nonnegative integer.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Number of matrix columns, specified as a nonnegative integer.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
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
Since R2025a
Output data type, specified as "double"
,"single"
, or "logical"
.
Output Arguments
Output matrix, returned as an all-zero sparse matrix.
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
Version History
Introduced before R2006a
You can specify the output data type by specifying the typename
argument as "double"
, "single"
, or"logical"
.