speye

Sparse identity matrix.

Category: Mathematics > Sparse Matrices

Syntax

S = speye(n)

Returns the n-by-n sparse identity.

S = speye(m, n)

Returns an m-by-n sparse matrix with ones on the main diagonal.

S = speye([m n])

Same, with the size given as a vector.

Description

speye(n) is the sparse counterpart of eye(n). It is the natural building block for shifted matrices such as A + sigma*speye(n), where using a full identity would destroy the sparsity of the sum.

Input arguments

m, n
Number of rows and columns.

Output arguments

S
Sparse matrix with min(m,n) stored nonzeros.

Example

Identity and a shifted matrix

I = speye(3)
full(speye(2, 3))
S = 2*speye(2) + sparse([0 1; 0 0]);
full(S)
Output
I = 

   (1,1)        1
   (2,2)        1
   (3,3)        1
ans = 2×3

     1     0     0
     0     1     0
ans = 2×2

     2     1
     0     2

Notes

MatLivre stores sparse matrices densely, so they save no memory, and sizes above 200 million elements are rejected. Results and display match MATLAB for ordinary sizes.

See also

eye sparse spones spdiags