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))`

.