sparse - Create codistributed sparse matrix - MATLAB (original) (raw)
Create codistributed sparse matrix
Syntax
Description
`S` = sparse([A](#mw%5Fec1207af-f2bc-475d-b664-71bfb473bbd5))
converts a full codistributed matrix to sparse form by removing any zero elements. You can save memory by converting a matrix that contains many zeros to sparse storage.
`S` = sparse([m,n](#mw%5Fc5dcddeb-fb15-4e1f-8729-dabfaff7d790))
creates an m
-by-n
codistributed sparse matrix of all zeros.
`S` = sparse([i,j](#mw%5Fdf4dab45-c268-4908-8863-061c93b14e88),[v](#mw%5Fc1013c71-0861-404b-b291-e0e78537b348))
creates a codistributed sparse matrix S
from the tripletsi
, j
, and v
. The number of rows in S
is set by the maximum value of i
, and the number of columns in S
is set by the maximum value ofj
. The matrix has space allotted for length(v)
nonzero elements.
Each of the inputs i
, j
, andv
must have either 1
or N
elements, such that each non-scalar input has the same number of elements.
S = sparse([i,j](#mw%5Fdf4dab45-c268-4908-8863-061c93b14e88),[v](#mw%5Fc1013c71-0861-404b-b291-e0e78537b348),[m,n](#mw%5Fc5dcddeb-fb15-4e1f-8729-dabfaff7d790))
specifies the size of S
asm
-by-n
.
S = sparse([i,j](#mw%5Fdf4dab45-c268-4908-8863-061c93b14e88),[v](#mw%5Fc1013c71-0861-404b-b291-e0e78537b348),[m,n](#mw%5Fc5dcddeb-fb15-4e1f-8729-dabfaff7d790),[nz](#mw%5F00fc0e50-994c-4a79-b526-abeede368754))
allocates space for nz
nonzero elements. Use this syntax to allocate extra space for nonzero values to be filled in after construction.
Examples
Create Codistributed Sparse Matrix
Create a 1000-by-1000 codistributed dense triangular matrix, distributed by its second dimension (columns). Convert the codistributed matrix into a codistributed sparse matrix.
spmd(4) C = triu(rand(1000,1000,"codistributed")); S = sparse(C); end
With four workers, each worker contains a 1000-by-250 local piece ofC
.
Input Arguments
A
— Input matrix
full codistributed
matrix | sparse codistributed
matrix
Input matrix, specified as a full or sparse codistributed
matrix. If A
is already sparse, then sparse(A)
returnsA
.
i,j
— Subscript pairs (as separate arguments)
scalar | vector | matrix
Subscript pairs, specified as separate arguments of scalars, vectors, or matrices. If i
and j
are not scalars,i(k)
, j(k)
, and v(k)
specify the value of S(i(k),j(k))
as:
If i
or j
is a scalar, the function uses that value to specify multiple elements in S
. For example if onlyi
is a scalar, j(k)
and v(k)
specify the value of S(i,j(k))
as:
If i
and j
have identical values for several elements in v, then sparse
aggregates the values in v
that have repeated indices. The aggregation behavior depends on the data type of the values in v
:
- For logical values,
sparse
applies theany
function. - For double values,
sparse
applies thesum
function.
v
— Values
scalar | vector | matrix
Values, specified as a scalar, vector, or matrix. The underlying type ofv
must be double
or logical
.
If v
is not a scalar, i(k)
,j(k)
, and v(k)
specify the value ofS(i(k),j(k))
as:
If v
is a scalar, the function uses that value to specify multiple elements in S
. For example if only v
is a scalar, i(k)
and j(k)
specify the value ofS(i(k),j(k))
as:
Any elements in v
that are zero are ignored, as are the corresponding subscripts in i
and j
.
sparse
sets the number of rows and columns in the output matrix before ignoring any zero elements in v
. Therefore, if you set any values in v
to 0, the size of the output matrix will not change.
m,n
— Size of each dimension (as separate arguments)
integer
Size of each dimension, specified as separate arguments of integers. The underlying type of m
and n
must be double
.m
is the row size and n is the column size. If you specifym
, you must specify n
.
If you do not specify m
and n
, thensparse
uses the default values m = max(i)
andn = max(j)
. These maxima are computed before any zeros inv
are removed.
nz
— Storage allocation for nonzero elements
nonnegative integer
Storage allocation for nonzero elements, specified as a nonnegative integer. The underlying type of m
and n
must bedouble
.
The default value is max([numel(i), numel(j), numel(v), 1])
.nz
must be greater than or equal to this value.
For the sparse matrix S
, the nnz function returns the number of nonzero elements in the matrix, and thenzmax function returns the amount of storage allocated for nonzero matrix elements. If nnz(S)
andnzmax(S)
return different results, then more storage might be allocated than is actually required. For this reason, set nz
only if you want to fill in values.
Version History
Introduced in R2006b