@makediagonal |

Create a matrix with vector placed on diagonal.

Syntax: @makediagonal(v, k)

v: vector, rowvector

k: (optional) integer

Return: sym or matrix

Create a square matrix with v placed in the k-th diagonal relative to the main diagonal, and zeros off the diagonal.

• If no k value is provided or if k is set to 0, the resulting sym matrix will have the same number of rows and columns as the length of v, and will have v in the main diagonal.

• If a value for k is provided, the matrix has the same number of rows and columns as the number of elements in the vector plus k, and will place v in the diagonal offset from the main by k.

Examples

sym s1 = @makediagonal(v1)

matrix m2 = @makediagonal(v1,1)

matrix m4 = @makediagonal(r1,-3)

S1 will contain V1 in the main diagonal; M2 will contain V1 in the diagonal immediately above the main diagonal; M4 will contain R1 in the diagonal 3 positions below the main diagonal. Using the optional k parameter may be useful in creating covariance matrices for AR models. For example, you can create an AR(1) correlation matrix by issuing the commands:

matrix(10,10) m1

vector(9) rho = .3

m1 = @makediagonal(rho,-1) + @makediagonal(rho,+1)

m1 = m1 + @identity(10)

Note that to make a diagonal matrix with the same elements on the diagonal, you may use @identity, multiplied by the scalar value.

Cross-references

See also
@identity.