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).
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,
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [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:
## [,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:
## [1] 2 3
The rows and columns can be given names using:
## col1 col2 col3
## row1 1 2 3
## row2 4 5 6
or,
## col1 col2 col3
## row1 1 2 3
## row2 4 5 6
The elements of a matrix can be extracted or subsetted as follows:
## [1] 2
## [1] 1
## col1 col2 col3
## 1 2 3
## row1 row2
## 2 5
To preserve the dimension attributes when subsetting, use the drop=FALSE
option:
## col1 col2 col3
## row1 1 2 3
## col2
## row1 2
## row2 5
\(\blacksquare\)
Vectors can be created in R using a variety of methods. The easiest
way is to use the combine function c()
:
## [1] 1 2 3
## [1] 1 2 3
## [1] 1 2 3
Vectors of numbers in R are of class "numeric"
and do
not have a dimension attribute:
## [1] "numeric"
## NULL
The elements of a vector can be assigned names using the names()
function:
## x1 x2 x3
## 1 2 3
To force a dimension attribute onto a vector, coerce it to a "matrix"
object using as.matrix()
:
## [,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]. \]
To take the transpose of a matrix or vector, use the t()
function:
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [,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\)
When specifying logical variables in R always spell out
TRUE
andFALSE
instead of usingT
andF
. Upon startup R defines the variablesT=TRUE
andF=FALSE
so thatT
andF
can be used as substitutes forTRUE
andFALSE
, respectively. However, this shortcut is not recommended because the variablesT
andF
could be reassigned during subsequent programming.↩︎