Mata is the new interactive matrix command language that comes with Stata 9. To start Mata, type meta in the Stata command window. The end a Mata session, type end and press return.
: A = (2,1\3,2\-2,2) : A 1 2 +-----------+ 1 | 2 1 | 2 | 3 2 | 3 | -2 2 | +-----------+
: B = 3*A : B 1 2 +-----------+ 1 | 6 3 | 2 | 9 6 | 3 | -6 6 | +-----------+
: B = (1,1\4,2\-2,1) : C = A + B : C 1 2 +-----------+ 1 | 3 2 | 2 | 7 4 | 3 | -4 3 | +-----------+ : D = A - B : D 1 2 +-----------+ 1 | 1 0 | 2 | -1 0 | 3 | 0 1 | +-----------+
: D = (2,1,3\-2,2,1) : C = D*A : C 1 2 +-----------+ 1 | 1 10 | 2 | 0 4 | +-----------+ : C = A*D : C 1 2 3 +----------------+ 1 | 2 4 7 | 2 | 2 7 11 | 3 | -8 2 -4 | +----------------+ : D = (2,1,3) : C = D*A : C 1 2 +-----------+ 1 | 1 10 | +-----------+ : C = A*D *: 3200 conformability error: - function returned error
: AT = A' : AT 1 2 3 +----------------+ 1 | 2 3 -2 | 2 | 1 2 2 | +----------------+ : ATT = AT' : ATT 1 2 +-----------+ 1 | 2 1 | 2 | 3 2 | 3 | -2 2 | +-----------+
: U = J(3,1,1) : U 1 +-----+ 1 | 1 | 2 | 1 | 3 | 1 | +-----+
: U = J(3,2,1) : U 1 2 +---------+ 1 | 1 1 | 2 | 1 1 | 3 | 1 1 | +---------+
: S = (2,1,4\3,2,2\-2,2,3) : S 1 2 3 +----------------+ 1 | 2 1 4 | 2 | 3 2 2 | 3 | -2 2 3 | +----------------+ : D = diag(S) : D [symmetric] 1 2 3 +-------------+ 1 | 2 | 2 | 0 2 | 3 | 0 0 3 | +-------------+ : V = (3,1,2) : D = diag(V) : D [symmetric] 1 2 3 +-------------+ 1 | 3 | 2 | 0 1 | 3 | 0 0 2 | +-------------+ : E = diagonal(S) : E 1 +-----+ 1 | 2 | 2 | 2 | 3 | 3 | +-----+
: I = I(3) : I [symmetric] 1 2 3 +-------------+ 1 | 1 | 2 | 0 1 | 3 | 0 0 1 | +-------------+
: C = (2,1,5\1,3,4\5,4,-2) : C [symmetric] 1 2 3 +----------------+ 1 | 2 | 2 | 1 3 | 3 | 5 4 -2 | +----------------+ : CT = C' : CT [symmetric] 1 2 3 +----------------+ 1 | 2 | 2 | 1 3 | 3 | 5 4 -2 | +----------------+
: A = (4,2,2 \ 4,6,8 \ -2,2,4) : A 1 2 3 +----------------+ 1 | 4 2 2 | 2 | 4 6 8 | 3 | -2 2 4 | +----------------+ : AI = luinv(A) : AI 1 2 3 +----------------------+ 1 | 1 -.5 .5 | 2 | -4 2.5 -3 | 3 | 2.5 -1.5 2 | +----------------------+
: C = (2,1,6\1,3,4\6,4,-2) : CI = invsym(C) : CI [symmetric] 1 2 3 +-------------------+ 1 | .6 | 2 | -.2 .4 | 3 | 0 0 0 | +-------------------+ : d = det(C) : d -102
: X = (3,2\2,-2\4,6\3,1) : X 1 2 +-----------+ 1 | 3 2 | 2 | 2 -2 | 3 | 4 6 | 4 | 3 1 | +-----------+ : r = rows(X) : c = cols(X) : r, c 1 2 +---------+ 1 | 4 2 | +---------+ : printf("rows = %f cols = %f", r, c) rows = 4 cols = 2
: A = (2,1\3,2\-2,2) : A 1 2 +-----------+ 1 | 2 1 | 2 | 3 2 | 3 | -2 2 | +-----------+ : cs = colsum(A) : cs 1 2 +---------+ 1 | 3 5 | +---------+ : rs = rowsum(A) : rs 1 +-----+ 1 | 3 | 2 | 5 | 3 | 0 | +-----+ : s = sum(A) : s 8
: cm = mean(A, 1) : cm 1 2 +-----------------------------+ 1 | 1 1.666666667 | +-----------------------------+ : rm = mean(A', 1) : rm 1 2 3 +-------------------+ 1 | 1.5 2.5 0 | +-------------------+
: A = (2,1\3,2\-2,2) : A 1 2 +-----------+ 1 | 2 1 | 2 | 3 2 | 3 | -2 2 | +-----------+ : B = (1,1\3,4\2,2) : B 1 2 +---------+ 1 | 1 1 | 2 | 3 4 | 3 | 2 2 | +---------+ : C = A,B : C 1 2 3 4 +---------------------+ 1 | 2 1 1 1 | 2 | 3 2 3 4 | 3 | -2 2 2 2 | +---------------------+
: = A\B : C 1 2 +-----------+ 1 | 2 1 | 2 | 3 2 | 3 | -2 2 | 4 | 1 1 | 5 | 3 4 | 6 | 2 2 | +-----------+
/* first create large matrix to be subset */ : A = (2,1\3,2\-2,2) : B = (1,1\3,4\5,0) : C = A,B\B,A : C 1 2 3 4 +---------------------+ 1 | 2 1 1 1 | 2 | 3 2 3 4 | 3 | -2 2 5 0 | 4 | 1 1 2 1 | 5 | 3 4 3 2 | 6 | 5 0 -2 2 | +---------------------+ /* create a matrix with 3kcolumns */ : D = C[1..., 1..3] : D 1 2 3 +----------------+ 1 | 2 1 1 | 2 | 3 2 3 | 3 | -2 2 5 | 4 | 1 1 2 | 5 | 3 4 3 | 6 | 5 0 -2 | +----------------+ /* create a matrix with 2 rows */ : D = C[1..2,1...] : D 1 2 3 4 +-----------------+ 1 | 2 1 1 1 | 2 | 3 2 3 4 | +-----------------+
Multivariate Course Page
Phil Ender, 22nov05, 4apr05