MTM
diag
extract the diagonals from a matrix or create a diagonal matrix
Calling Sequence
Parameters
Description
Examples
diag(A)
diag(A, k)
A
-
matrix, vector, array, or scalar
k
(optional) integer
The diag command either creates a matrix with entries on the diagonal, or extracts the entries from the diagonal of a matrix.
The diag(A) command returns a matrix with the entries of A along the main diagonal, and 0 everywhere else when A is a vector, or a matrix with one row or column.
The diag(A, k) command returns a matrix with the entries of A along the kth diagonal specified by the selection parameter k, and 0 everywhere else, when A is a vector, or a matrix with one row or column.
For a matrix A with more than one row and more than one column, the diag(A) command returns the main diagonal of A as a (column) vector.
For a matrix A with more than one row and more than one column, the diag(A, k) command returns the (column) vector corresponding to the diagonal of A, specified by the selection parameter k.
The diagonals of a matrix are indexed using signed integers, where the main diagonal has index 0. Superdiagonals are indexed with positive integers and subdiagonals are indexed with negative integers.
with⁡MTM:
A≔Matrix⁡1,2,1,4,5,6,2,8,1
A≔121456281
diag⁡A
151
diag⁡A,1
26
diag⁡A,−1
48
B≔Vector⁡4,5,6
B≔456
diag⁡B
400050006
diag⁡B,1
0400005000060000
diag⁡B,−1
0000400005000060
See Also
ArrayTools[Diagonal]
LinearAlgebra[Diagonal]
LinearAlgebra[DiagonalMatrix]
MTM[tril]
MTM[triu]
Download Help Document