1 |
|
2 |
|
3 | # Function identity
|
4 |
|
5 | Create a 2-dimensional identity matrix with size m x n or n x n.
|
6 | The matrix has ones on the diagonal and zeros elsewhere.
|
7 |
|
8 |
|
9 | ## Syntax
|
10 |
|
11 | ```js
|
12 | math.identity(n)
|
13 | math.identity(n, format)
|
14 | math.identity(m, n)
|
15 | math.identity(m, n, format)
|
16 | math.identity([m, n])
|
17 | math.identity([m, n], format)
|
18 | ```
|
19 |
|
20 | ### Parameters
|
21 |
|
22 | Parameter | Type | Description
|
23 | --------- | ---- | -----------
|
24 | `size` | ...number | Matrix | Array | The size for the matrix
|
25 | `format` | string | The Matrix storage format
|
26 |
|
27 | ### Returns
|
28 |
|
29 | Type | Description
|
30 | ---- | -----------
|
31 | Matrix | Array | number | A matrix with ones on the diagonal.
|
32 |
|
33 |
|
34 | ## Examples
|
35 |
|
36 | ```js
|
37 | math.identity(3) // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
38 | math.identity(3, 2) // returns [[1, 0], [0, 1], [0, 0]]
|
39 |
|
40 | const A = [[1, 2, 3], [4, 5, 6]]
|
41 | math.identity(math.size(A)) // returns [[1, 0, 0], [0, 1, 0]]
|
42 | ```
|
43 |
|
44 |
|
45 | ## See also
|
46 |
|
47 | [diag](diag.md),
|
48 | [ones](ones.md),
|
49 | [zeros](zeros.md),
|
50 | [size](size.md),
|
51 | [range](range.md)
|