Function identity #

Create a 2-dimensional identity matrix with size m x n or n x n. The matrix has ones on the diagonal and zeros elsewhere.

Syntax #

math.identity(n)
math.identity(n, format)
math.identity(m, n)
math.identity(m, n, format)
math.identity([m, n])
math.identity([m, n], format)

Parameters #

Parameter Type Description
size …number | Matrix | Array The size for the matrix
format string The Matrix storage format

Returns #

Type Description
Matrix | Array | number A matrix with ones on the diagonal.

Examples #

math.identity(3)                    // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
math.identity(3, 2)                 // returns [[1, 0], [0, 1], [0, 0]]

const A = [[1, 2, 3], [4, 5, 6]]
math.identity(math.size(A))         // returns [[1, 0, 0], [0, 1, 0]]

See also #

diag, ones, zeros, size, range

Fork me on GitHub