Unit1 Matrix and Array
Unit1 Matrix and Array
Matrix
A matrix is simply several vectors stored together. Whereas the size of a vector
is described by its length, the size of a matrix is specified by a number of rows
and a number of columns. You typically describe a matrix A as an m*n matrix;
that is, A will have exactly m rows and n columns. This means A will have a
total of m n entries, with each entry ai,j having a unique position given by its
specific row (i = 1, 2, ……, m) and column ( j = 1, 2, ….., n). You can
therefore express a matrix as follows:
To create a matrix in R, use the matrix command, providing the entries of the
matrix to the data argument as a vector:
R> A
[,1] [,2]
[1,] -3 893.00
[2,] 2 0.17
You must make sure that the length of this vector matches exactly with the
number of desired rows (nrow) and columns (ncol). You can elect not to supply
nrow and ncol when calling matrix, in which case R’s default behavior is to
return a single-column matrix of the entries in data. For example, matrix
(data=c(-3,2,893,0.17)) would be identical to matrix(data=c(-3,2,893,0.17),
nrow=4, ncol=1).
Filling Direction
It’s important to be aware of how R fills up the matrix using the entries from
data. Looking at the previous example, you can see that the 2*2 matrix A has
been filled in a column-by-column fashion when reading the data entries from
left to right. You can control how R fills in data using the argument byrow, as
shown in the following examples:
[1,] 1 3 5
[2,] 2 4 6
Here a 2*3 matrix containing the digits 1 through 6. By using the optional
argument byrow and setting it to FALSE, you explicitly tell R to fill this 2*3
structure in a column-wise fashion, by filling each column before moving to
the next, reading the data argument vector from left to right. This is R’s default
handling of the matrix function, so if the byrow argument isn’t supplied, the
software will assume byrow=FALSE. Now, let’s repeat the same line of code
but set byrow=TRUE.
R> matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
[1,] 1 2 3
[2,] 4 5 6
The resulting 2*3 structure has now been filled in a row-wise fashion.
Row and Column Bindings
If you have multiple vectors of equal length, you can quickly build a matrix
by binding together these vectors using the built-in R functions, rbind and
cbind. You can either treat each vector as a row (by using the command
rbind) or treat each vector as a column (using the command cbind). Let you
have the two vectors 1:3 and 4:6. You can reconstruct the 2*3 matrix using
rbind as follows:
R> rbind(1:3,4:6)
[1,] 1 2 3
[2,] 4 5 6
Here, rbind has bound together the vectors as two rows of a matrix, with the top-
to-bottom order of the rows matching the order of the vectors supplied to rbind.
The same matrix could be constructed as follows, using cbind:
R> cbind(c(1,4),c(2,5),c(3,6))
[1,] 1 2 3
[2,] 4 5 6
Here, you have three vectors each of length 2. You use cbind to glue together
these three vectors in the order they were supplied, and each vector becomes
a column of the resulting matrix.
Matrix Dimensions
R> mymat
Having defined a matrix mymat using rbind, you can confirm its dimensions
with dim, which returns a vector of length 2; dim always supplies the number
of rows first, followed by the number of columns. You can also use two related
functions: nrow (which provides the number of rows only) and ncol (which
provides the number of columns only). In the last command shown, you use
dim and your knowledge of vector subsetting to extract the same result that
ncol would give you.
Subsetting
Extracting and subsetting elements from matrices in R is much like extracting
elements from vectors. The only complication is that you now have an
additional dimension. Element extraction still uses the square-bracket operator,
but now it must be performed with both a row and a column position, given
strictly in the order of [row,column]. Let’s start by creating a 3*3 matrix.
R>
A<- matrix(c(0.3,4.5,55.3,91,0.1,105.5,4.2,8.2,27.9),nrow=3,ncol=3)
R> A
To tell R to “look at the third row of A and give me the element from the second
column,” you execute the following:
R> A[3,2]
[1] 105.5
R> A[,2]
91.0 0.1 105.5
R> A[1,]
You can also identify the values along the diagonal of a square matrix (that is, a
matrix with an equal number of rows and columns) using the diag command.
R> diag(x=A)
To delete or omit elements from a matrix, you again use square brackets, but this
time with negative indexes. The following provides A without its second
column:
R> A[,-2]
[,1] [,2]
[1,] 0.3 -4.2
R> B<-A
R> B
The following overwrites the second row of B with the sequence 1, 2, and
3:
R> B[2,] <- 1:3
R> B
[,1] [,2] [,3]
[1,] 0.3 91.0 -4.2
The following overwrites the second column elements of the first and third
rows with 900:
R> B
For any m*n matrix A, its transpose, AT, is the n*m matrix obtained by
writing either its columns as rows or its rows as columns.
In R, the transpose of a matrix is found with the function t. Let’s create a new
matrix and then transpose it.
R> A <- rbind(c(2,5,2),c(6,1,4))
R> A
[,1] [,2] [,3]
[1,] 2 5 2
[2,] 6 1 4
R> t(A)
[,1] [,2]
[1,] 2 6
[2,] 5 1
[3,] 2 4
[1,] 2 5 2
[2,] 6 1 4
Identity Matrix
R> A
R> a <- 2
R> a*A
[1,] 4 10 4
[2,] 12 2 8
Matrix Addition and Subtraction
You can add or subtract any two equally sized matrices with the standard
and - symbols.
R> A
[,1] [,2]
[1,] 2 6
[2,] 5 1
[3,] 2 4
R> B
[,1] [,2]
[1,] -2 8.1
[2,] 3 8.2
[3,] 6 -9.8
R> A-B
[,1] [,2]
[1,] 4 -2.1
[2,] 2 -7.2
[3,] -4 13.8
Matrix Multiplication
R> dim(A)
[1]2 3
[1]3 2
This confirms the two matrices are compatible for multiplication, so you can
proceed.
R> A%*%B
[,1] [,2]
[1,] 3 9
[2,] 21 3
You can show that matrix multiplication is non commutative using the same
two matrices. Switching the order of multiplication gives you an entirely
different result.
R> B%*%A
[1,] -12 12 -6
[2,] 4 -4 2
[3,] 32 10 22
Multidimensional Arrays
Just as a matrix (a “rectangle” of elements) is the result of increasing the
dimension of a vector (a “line” of elements), the dimension of a matrix can
be increased to get more complex data structures. In R, vectors and matrices
can be considered special cases of the more general array, which is how I’ll
refer to these types of structures when they have more than two dimensions.
The index of each element is given at the corresponding position. These
indexes are provided in the strict order of [row,column,layer].
To create these data structures in R, use the array function and specify
the individual elements in the data argument as a vector. Then specify
size in the dim argument as another vector with a length corresponding
to the number of dimensions. Note that array fills the entries of each layer
with the elements in data in a strict column-wise fashion, starting with
the first layer. Consider the following example:
R> AR <- array(data=1:24,dim=c(3,4,2))
R> AR
,,1
,,2
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
This gives you an array of the same size—each of the two layers constitutes
a 3*4 matrix. In this example, note the order of the dimensions supplied to
dim: c(rows,columns,layers). Just like a single matrix, the product of the
dimension sizes of an array will yield the total number of elements. As you
increase the dimension further, the dim vector must be extended
accordingly. For example, a four-dimensional array is the next step up and
can be thought of as blocks of three-dimensional arrays. Suppose you had a
four-dimensional array comprised of three copies of AR, the three-
dimensional array just defined. This new array can be stored in R as follows
(once again, the array is filled column-wise):
,,1,1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
,,2,1
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
,,1,2
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
,,2,2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
,,1,3
,,2,3
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24