## 3.1 Matrices and Vectors

A matrix is just an array of numbers. The dimension of a matrix is determined by the number of its rows and columns. For example, a matrix $$\mathbf{A}$$ with $$n$$ rows and $$m$$ columns is illustrated below: $\underset{(n\times m)}{\mathbf{A}}=\left[\begin{array}{cccc} a_{11} & a_{12} & \ldots & a_{1m}\\ a_{21} & a_{22} & \ldots & a_{2m}\\ \vdots & \vdots & \ddots & \vdots\\ a_{n1} & a_{n2} & \ldots & a_{nm} \end{array}\right]$ where $$a_{ij}$$ denotes the $$i^{th}$$ row and $$j^{th}$$ column element of $$\mathbf{A}$$.

A vector is simply a matrix with $$1$$ column. For example, $\underset{(n\times1)}{\mathbf{x}}=\left[\begin{array}{c} x_{1}\\ x_{2}\\ \vdots\\ x_{n} \end{array}\right]$ is an $$n\times1$$ vector with elements $$x_{1},x_{2},\ldots,x_{n}$$. Vectors and matrices are often written in bold type (or underlined) to distinguish them from scalars (single elements of vectors or matrices).

Example 3.1 (Matrix creation in R)

In R, matrix objects are created using the matrix() function. For example, to create the $$2\times3$$ matrix: $\underset{(2\times3)}{\mathbf{A}}=\left[\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6 \end{array}\right]$ use,

matA = matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
matA
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
class(matA)
## [1] "matrix" "array"

The optional argument byrow=TRUE fills the matrix row-by-row.13 The default is byrow=FALSE which fills the matrix column-by-column:

matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3)
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6

Matrix objects (i.e., objects of class "matrix") have row and column dimension attributes which can be examined with the dim() function:

dim(matA)
## [1] 2 3

The rows and columns can be given names using:

dimnames(matA) = list(c("row1","row2"),c("col1","col2","col3"))
matA
##      col1 col2 col3
## row1    1    2    3
## row2    4    5    6

or,

colnames(matA) = c("col1", "col2", "col3")
rownames(matA) = c("row1", "row2")
matA
##      col1 col2 col3
## row1    1    2    3
## row2    4    5    6

The elements of a matrix can be extracted or subsetted as follows:

matA[1, 2]
## [1] 2
matA["row1", "col1"]
## [1] 1
matA[1, ]
## col1 col2 col3
##    1    2    3
matA[, 2]
## row1 row2
##    2    5

To preserve the dimension attributes when subsetting, use the drop=FALSE option:

matA[1, , drop=FALSE]
##      col1 col2 col3
## row1    1    2    3
matA[, 2, drop=FALSE]
##      col2
## row1    2
## row2    5

$$\blacksquare$$

Example 3.2 (Creating vectors in R)

Vectors can be created in R using a variety of methods. The easiest way is to use the combine function c():

xvec = c(1,2,3)
xvec
## [1] 1 2 3
xvec = 1:3
xvec
## [1] 1 2 3
xvec = seq(from=1,to=3,by=1)
xvec
## [1] 1 2 3

Vectors of numbers in R are of class "numeric" and do not have a dimension attribute:

class(xvec)
## [1] "numeric"
dim(xvec)
## NULL

The elements of a vector can be assigned names using the names() function:

names(xvec) = c("x1", "x2", "x3")
xvec
## x1 x2 x3
##  1  2  3

To force a dimension attribute onto a vector, coerce it to a "matrix" object using as.matrix():

as.matrix(xvec)
##    [,1]
## x1    1
## x2    2
## x3    3

$$\blacksquare$$

The transpose of an $$n\times m$$ matrix $$\mathbf{A}$$ is a new matrix with the rows and columns of $$\mathbf{A}$$ interchanged, and is denoted $$\mathbf{A}^{\prime}$$ or $$\mathbf{A}^{\intercal}$$. For example, \begin{align*} \underset{(2\times3)}{\mathbf{A}} & =\left[\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6 \end{array}\right],~\underset{(3\times2)}{\mathbf{A}^{\prime}}=\left[\begin{array}{cc} 1 & 4\\ 2 & 5\\ 3 & 6 \end{array}\right]\\ \underset{(3\times1)}{\mathbf{x}} & =\left[\begin{array}{c} 1\\ 2\\ 3 \end{array}\right],~\underset{(1\times3)}{\mathbf{x}^{\prime}}=\left[\begin{array}{ccc} 1 & 2 & 3\end{array}\right]. \end{align*}

A symmetric matrix $$\mathbf{A}$$ is such that $$\mathbf{A}=\mathbf{A}^{\prime}.$$ Obviously, this can only occur if $$\mathbf{A}$$ is a square matrix; i.e., the number of rows of $$\mathbf{A}$$ is equal to the number of columns. For example, consider the $$2\times2$$ matrix: $\mathbf{A}=\left[\begin{array}{cc} 1 & 2\\ 2 & 1 \end{array}\right].$ Then, $\mathbf{A}^{\prime}=\mathbf{A}=\left[\begin{array}{cc} 1 & 2\\ 2 & 1 \end{array}\right].$

Example 2.9 (Transpose of a matrix in R)

To take the transpose of a matrix or vector, use the t() function:

matA = matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
matA
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
t(matA)
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
xvec = c(1,2,3)
t(xvec)
##      [,1] [,2] [,3]
## [1,]    1    2    3

Notice that, when applied to a "numeric" vector with $$n$$ elements, the t() function returns a "matrix" object with dimension $$1\times n$$.

$$\blacksquare$$

1. When specifying logical variables in R always spell out TRUE and FALSE instead of using T and F. Upon startup R defines the variables T=TRUE and F=FALSE so that T and F can be used as substitutes for TRUE and FALSE, respectively. However, this shortcut is not recommended because the variables T and F could be reassigned during subsequent programming.↩︎