11.07.2015 Views

[U] User's Guide

[U] User's Guide

[U] User's Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

174 [ U ] 14 Matrix expressions7. When defining an element of a matrix, usematrix matname[i,j] = expressionwhere i and j are scalar expressions. The matrix matname must already exist.Example:matrix A = J(2,2,0)matrix A[1,2] = sqrt(2)8. To replace a submatrix within a matrix, use the same syntax. If the expression on the right evaluatesto a scalar or 1 × 1 matrix, the element is replaced. If it evaluates to a matrix, the submatrix withtop-left element at (i, j) is replaced. The matrix matname must already exist.Example:matrix A = J(4,4,0)matrix A[2,2] = C’*C14.10 Using matrices in scalar expressionsScalar expressions are documented as exp in the Stata manuals:generate newvar = exp if exp . . .replace newvar = exp if exp . . .regress . . . if exp . . .if exp {. . . }while exp {. . . }Most importantly, scalar expressions occur in generate and replace, in the if exp modifier allowedon the end of many commands, and in the if and while commands for program control.You will rarely need to refer to a matrix in any of these situations except when using the if andwhile commands.In any case, you may refer to matrices in any of these situations, but the expression cannot requireevaluation of matrix expressions returning matrices. Thus you could refer to trace(A) but not totrace(A+B).It can be difficult to predict when an evaluation of an expression requires evaluating a matrix;even experienced users can be surprised. If you get the error message “matrix operators that returnmatrices not allowed in this context”, r(509), you have encountered such a situation.toThe solution is to split the line in two. For instance, you would changeor even toif trace(A+B)==0 {. . .}matrix AplusB = A+Bif trace(AplusB)==0 {. . .}matrix Trace = trace(A+B)if Trace[1,1]==0 {. . .}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!