Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Create diagonal matrix or get diagonal elements of matrix

`D = diag(v)`

`D = diag(v,k)`

`x = diag(A)`

`x = diag(A,k)`

`D = diag(`

returns
a square diagonal matrix with the elements of vector `v`

)`v`

on
the main diagonal.

The

`trace`

of a matrix is equal to`sum(diag(A))`

.