speye - Sparse identity matrix - MATLAB (original) (raw)
Syntax
Description
[S](#mw%5Fba0101af-b06b-4993-bfe5-b5455d62eda3) = speye
returns a sparse scalar1
.
[S](#mw%5Fba0101af-b06b-4993-bfe5-b5455d62eda3) = speye([n](#mw%5Ffac7013e-940b-47c3-8d3f-e834dbc84f3e))
returns a sparse n
-by-n
identity matrix, with ones on the main diagonal and zeros elsewhere.
[S](#mw%5Fba0101af-b06b-4993-bfe5-b5455d62eda3) = speye([n](#mw%5Ffac7013e-940b-47c3-8d3f-e834dbc84f3e),[m](#mw%5Ffac7013e-940b-47c3-8d3f-e834dbc84f3e))
returns a sparse n
-by-m
matrix, with ones on the main diagonal and zeros elsewhere.
[S](#mw%5Fba0101af-b06b-4993-bfe5-b5455d62eda3) = speye([sz](#mw%5F84834ae2-7fa8-46f4-a47e-4f417c5028aa))
returns a matrix with ones on the main diagonal and zeros elsewhere. The size vectorsz
defines size(S)
. For example, speye([2 3])
returns a 2-by-3 matrix.
[S](#mw%5Fba0101af-b06b-4993-bfe5-b5455d62eda3) = speye(___,[typename](#mw%5F34566fc1-cedf-4556-a6cb-92831bc3157e))
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 a 1000-by-1000 square sparse identity matrix and view the sparsity pattern.
The result is the same as sparse(eye(1000))
, but this latter version requires temporary storage for the full representation before it is converted to sparse storage.
Create a 400-by-800 sparse identity matrix and view the pattern of nonzeros.
S = speye(400,800); spy(S)
This command is equivalent to speye([400 800])
.
Input Arguments
Dimension sizes, specified as nonnegative integer scalars.
- If
n
is the only input argument, thenS
is ann
-by-n
identity matrix. - If
n
orm
is0
, thenS
is an empty matrix. - If
n
orm
is negative, then it is treated as0
.
Example: speye(4)
creates a 4-by-4 identity matrix.
Example: speye(3,6)
creates a 3-by-6 identity matrix.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Since R2025a
Output data type, specified as "double"
,"single"
, or "logical"
.
Dimension sizes, specified as a two-element row vector. The row vector is of the form [numRows numCols]
.
- If an element of
sz
is0
, thenS
is an empty matrix. - If an element of
sz
is negative, then the element is treated as0
.
Example: speye([4 5])
creates a 4-by-5 matrix and is equivalent tospeye(4,5)
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
Output matrix, returned as a sparse identity matrix.
Extended Capabilities
The speye
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:S = gpuArray.speye
.S = gpuArray.speye(n)
.S = gpuArray.speye(n,m)
.S = gpuArray.speye(sz)
.S = gpuArray.speye(___,typename)
, wheretypename
specifies the data type in addition to the input argument combinations in any of the 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"
, "single"
, or"logical"
.