Stata 11 help for mf_diag

help mata diag() -------------------------------------------------------------------------------

Title

[M-5] diag() -- Create diagonal matrix

Syntax

numeric matrix diag(numeric matrix Z)

numeric matrix diag(numeric vector z)

Description

diag() creates diagonal matrices.

diag(Z), Z a matrix, extracts the principal diagonal of Z to create a new matrix. Z must be square.

diag(z), z a vector, creates a new matrix with the elements of z on its diagonal.

Remarks

Do not confuse diag() with its functional inverse, diagonal(); see [M-5] diagonal(). diag() creates a matrix from a vector (or matrix); diagonal() extracts the diagonal of a matrix into a vector.

Use of diag() should be avoided because it wastes memory. The colon operators will allow you to use vectors directly:

Desired calculation Equivalent --------------------------------------------- diag(v)*X, v a column v:*X v a row v':*X v a matrix diagonal(v):*X

X*diag(v) v a column X:*v' v a row X:*v v a matrix X:*diagonal(v)' ---------------------------------------------

In the above table, it is assumed that v is real. If v might be complex, the transpose operators that appear must be changed to transposeonly() calls, because we do not want the conjugate. For instance, v':*X would become transposeonly(v):*X.

Conformability

diag(Z): Z: m x n result: min(m,n) x min(m,n)

diag(z): z: 1 x n or n x 1 result: n x n

Diagnostics

None.

Source code

diag.mata

Also see

Manual: [M-5] diag()

Help: [M_5] _diag(), [M-5] diagonal(), [M-5] isdiagonal(); [M-4] manipulation


© Copyright 1996–2009 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index