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.

example

[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.

example

[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.

example

[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

collapse all

Create a 1000-by-1000 square sparse identity matrix and view the sparsity pattern.

Figure contains an axes object. The axes object with xlabel nz = 1000 contains a line object which displays its values using only markers.

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)

Figure contains an axes object. The axes object with xlabel nz = 400 contains a line object which displays its values using only markers.

This command is equivalent to speye([400 800]).

Input Arguments

collapse all

Dimension sizes, specified as nonnegative integer scalars.

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].

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

collapse all

Output matrix, returned as a sparse identity matrix.

Extended Capabilities

expand all

The speye function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

You can specify the output data type by specifying the typename argument as "double", "single", or"logical".