Diagonal
Examples
open all close allBasic Examples (4)
Give the diagonal elements of a matrix:
{{a, b, c}, {d, e, f}, {g, h, i}}//MatrixFormDiagonal[%]Diagonal[{{a, b, c}, {d, e, f}, {g, h, i}}, 1]Diagonal[{{a, b, c}, {d, e, f}, {g, h, i}}, -1]Give a diagonal of a nonsquare matrix:
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}//MatrixFormDiagonal[%]Scope (12)
Basic Uses (7)
Find the diagonal of a machine-precision matrix:
Diagonal[{{1.1, 12.2, 3.23}, {2.3, 42.2, 35.3}, {1.2, 3.1, 2.3}}]The superdiagonal of a complex matrix:
Diagonal[{{1. + I, 2, 3 - 2 I}, {0, 4 π, 5I}, {E, 0, 6}}, 1]Diagonal[{{2, 3, 1}, {2, 2, 1}, {3, 1, 2}}]Diagonal of an arbitrary-precision matrix:
Diagonal[RandomReal[1, {3, 3}, WorkingPrecision -> 20]]The diagonal of a symbolic matrix that is two below the main diagonal:
Diagonal[{{a, b, c, d}, {e, f, g, h}, {i, j, k, l}, {m, n, o, p}}, -2]Diagonal accepts non-square matrices:
{{3, 2, 2}, {2, 3, -2}, {4, 2, 1}, {3, 7, 9}}//MatrixFormDiagonal[%]Extraction of the diagonal of a large matrix is efficient:
m = RandomReal[{1, 9}, {50, 100}];Diagonal[m] //TimingSpecial Matrices (5)
The diagonal of a sparse matrix is returned as a sparse list:
SparseArray[{{1, 1} -> 12, {3, 2} -> 32, {3, 3} -> 33}, {3, 3}]Diagonal[%]Convert the result to an ordinary list:
Normal[%]Get all the diagonals of the sparse array:
s = SparseArray[{Band[{1, 1}] -> x, Band[{2, 1}] -> y, Band[{1, 2}] -> z}, {3, 3}];s//MatrixFormTable[Diagonal[s, k], {k, -2, 2}]Convert the results to an ordinary list:
Normal[%]The diagonals of structured matrices:
SymmetrizedArray[{{1, 2} -> 2, {2, 2} -> I, {3, 1} -> 4, {4, 4} -> 2}, {4, 4}, Symmetric[All]]Diagonal[%]QuantityArray[{{1, 2}, {3, 4}, {5, 6}}, {"Meters", "Seconds"}]Diagonal[%]IdentityMatrix has a diagonal of all ones:
Diagonal[IdentityMatrix[15]]Diagonal of HilbertMatrix:
Diagonal[HilbertMatrix[13]]Applications (3)
Express a matrix as the sum of its diagonal and off-diagonal parts:
MatrixForm[m = Array[Subscript[a, ##]&, {4, 4}]]md = DiagonalMatrix[Diagonal[m]];Construct the diagonal as the difference between the original matrix and its diagonal part:
mo = m - md;Confirm that two matrices have the desired properties:
Map[MatrixForm, {md, mo}]Determine if the matrix
is diagonalizable using its Jordan decomposition:
m = {{27, 48, 81}, {-6, 0, 0}, {1, 0, 3}};{s, j} = JordanDecomposition[m]The superdiagonal of the Jordan form
does not consist solely of zeros, so
is not diagonalizable:
Diagonal[j, 1]Confirm with a direct call to DiagonalizableMatrixQ:
DiagonalizableMatrixQ[m]Find the eigenvalues of the matrix
using its Jordan decomposition:
m = {{27, 48, 81}, {-6, 0, 0}, {1, 0, 3}};{s, j} = JordanDecomposition[m]The diagonal of the Jordan form
gives the eigenvalues:
Diagonal[j]Confirm with a direct call to Eigenvalues:
Eigenvalues[m]Properties & Relations (7)
For square m, DiagonalMatrix[Diagonal[m]]==m iff DiagonalMatrixQ[m] is True:
m = (| | |
| - | - |
| a | 0 |
| 0 | d |);{DiagonalMatrix[Diagonal[m]] == m, DiagonalMatrixQ[m]}m = (| | |
| - | - |
| 1 | 2 |
| 3 | 4 |);{DiagonalMatrix[Diagonal[m]] == m, DiagonalMatrixQ[m]}For a matrix m, Tr[m] can be expressed as a combination of Diagonal and Total:
m = RandomReal[1, {5, 5}];Tr[m] == Total[Diagonal[m]]Diagonal[m,k] for an n×n matrix gives non-empty results for 1-n<=k<=n-1:
(m = Table[j - i, {i, 4}, {j, 4}])//MatrixFormTable[Diagonal[m, k], {k, -4, 4}]Diagonal[m,k] gives the lowest nonzero diagonal of UpperTriangularize[m,k]:
m = (| | | | |
| - | - | - | - |
| 3 | 8 | 7 | 6 |
| 8 | 8 | 1 | 3 |
| 8 | 6 | 6 | 8 |
| 1 | 7 | 3 | 7 |);{Diagonal[m, 2], UpperTriangularize[m, 2]//MatrixForm}Similarly, Diagonal[m,k] gives the highest nonzero diagonal of LowerTriangularize[m,k]:
{Diagonal[m, 1], LowerTriangularize[m, 1]//MatrixForm}A matrix can be reconstructed from its diagonals using Band:
m = RandomReal[1, {5, 5}];m == SparseArray[Table[Band[If[k >= 0, {1, 1 + k}, {1 - k, 1}]] -> Diagonal[m, k], {k, -4, 4}]]For a matrix m, Diagonal[m] is equivalent to Tr[m,List]:
m = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |);Diagonal[m]Tr[m, List]For a square matrix m, Diagonal[m] is equivalent to Transpose[m,{1,1}]:
m = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |);Diagonal[m]Transpose[m, {1, 1}]See Also
DiagonalMatrix DiagonalMatrixQ Tr Band LowerTriangularize UpperTriangularize
Function Repository: Antidiagonal ZeroDiagonal
Related Guides
History
Text
Wolfram Research (2007), Diagonal, Wolfram Language function, https://reference.wolfram.com/language/ref/Diagonal.html.
CMS
Wolfram Language. 2007. "Diagonal." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Diagonal.html.
APA
Wolfram Language. (2007). Diagonal. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Diagonal.html
BibTeX
@misc{reference.wolfram_2026_diagonal, author="Wolfram Research", title="{Diagonal}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Diagonal.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_diagonal, organization={Wolfram Research}, title={Diagonal}, year={2007}, url={https://reference.wolfram.com/language/ref/Diagonal.html}, note=[Accessed: 13-June-2026]}