# mxMatrix Help

## Primary tabs

#### Matrix Types

Matrices in OpenMx must be one of:

 m11 0 0 0 m22 0 0 0 m33

Full

 m11 m21 m31 m21 m22 m32 m31 m32 m33
 1 0 0 0 1 0 0 0 1

Lower

 m11 0 0 m21 m22 0 m31 m32 m33

Stand (symmetric, ones on diagonal)

 1 m21 m31 m21 1 m32 m31 m32 1

Sdiag (the sub-diagonal lower matrix)

 0 0 0 m21 0 0 m31 m32 0
 m11 m21 m31 m21 m22 m32 m31 m32 m33
 1 1 1 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0

#### Basic Examples

    require(OpenMx)
A     <- mxMatrix('Full', 1, 1, values = 1, name = 'A')
B     <- mxMatrix('Full', 1, 1, values = 2, name = 'B')
C     <- mxMatrix('Full', 1, 1, values = 3, name = 'C')
D     <- mxMatrix('Full', 3, 1, labels = c('A[1,1]', 'B[1,1]', 'C[1,1]'), name = 'D')
model <- mxModel('model', A, B, C, D)
fit   <- mxRun(model)
mxEval(D, fit)

    A <- mxMatrix('Full', 2, 2, values = c(1,2,3,4), byrow = TRUE, name = 'A')
B <- mxAlgebra(A + A, name = 'B')
C <- mxMatrix('Full', 2, 2, labels = c('B[2,2]', 'B[2,1]', 'B[1,2]', 'B[1,1]'), byrow = TRUE, name = 'C')
model <- mxModel('model', A, B, C)
fit   <- mxRun(model)
mxEval(C, fit)


#### Get the row or column count of an mxMatrix

    myMatrix <- mxMatrix('Full', 3, 5)
nrow(myMatrix)
ncol(myMatrix)


#### Square bracket substitution to set labels

This feature allows you to use labels in an MxMatrix to refer to other MxMatrices or to MxAlgebras. This is done by specifying the name, row, and col of the target matrix or algebra as you would expect to be able to in R, i.e., "foo[row,col]".

The effect of pointing to another cell using bracket notation is just the same as setting the label of the two cells to the same value.

So, given a matrix A with labels:

    aLabels = c(
"a1", "a2",
"a3", "a4"
)


Setting the labels of matrix B to this:

    bLabels = c(
"a1", "b2",
"b3", "b4"
)


Is the same as setting the labels of matrix B to this:

    bLabels = c(
"A[1,1]", "b2",
"b3",     "b4"
)


Both methods forces cell B[1,1] to take the same value as A[1,1].
the benefit of the [] syntax is that you only need to set one label to equate two cells, and you don't need to know what label the other cell has currently, just its location.

#### Gotchas

You might see examples (for instance univariate examples) where labels are set with a one item, i.e

 mxMatrix('Full', 1, 1, labels = "A11", name = 'A')

Bear in mind that if you scale this up to a multivariate case, e.g.:

 mxMatrix('Full', nrow=2, ncol=2, labels = "A11", name = 'A')

All 4 cells of the new matrix will have the same label, and therefore their values will be forced to be equal. This is probably not what you wanted.
The solution is to provide labels for all the cells

 mxMatrix('Full', nrow=2, ncol=2, labels = c("A11","A21","A12","A22",) name = 'A')

or for those more familiar with R:


alabels = paste("A", rep(1:n,each=n), 1:n, sep="");
mxMatrix('Full', nrow=n, ncol=n, labels = alabels, name = 'A')

A@labels
[,1]  [,2]  [,3]
[1,] "A11" "A21" "A31"
[2,] "A12" "A22" "A32"
[3,] "A13" "A23" "A33"