0% found this document useful (0 votes)
32 views

Unit1 Matrix and Array

The document discusses matrix operations and algebra in R. It defines what a matrix is and how to create, subset, and manipulate matrices using functions like matrix(), rbind(), cbind(), dim(), and t(). It also covers mathematical matrix operations like transposition and how to create an identity matrix using diag(). Key points are that a matrix has rows and columns and is filled either row-wise or column-wise, and you can extract elements using row and column indexes in square brackets.

Uploaded by

Shreya Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Unit1 Matrix and Array

The document discusses matrix operations and algebra in R. It defines what a matrix is and how to create, subset, and manipulate matrices using functions like matrix(), rbind(), cbind(), dim(), and t(). It also covers mathematical matrix operations like transposition and how to create an identity matrix using diag(). Key points are that a matrix has rows and columns and is filled either row-wise or column-wise, and you can extract elements using row and column indexes in square brackets.

Uploaded by

Shreya Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit 1: Matrix Operations and Algebra, Multidimensional 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 <- matrix (data=c(-3, 2, 893, 0.17), nrow=2, ncol=2)

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:

R> matrix(data=c(1, 2, 3, 4, 5, 6),nrow=2, ncol=3,


byrow=FALSE)

[,1] [,2] [,3]

[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] [,2] [,3]

[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] [,2] [,3]

[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] [,2] [,3]

[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

Another useful function, dim, provides the dimensions of a matrix stored in


your workspace.

R> mymat <- rbind(c(1,3,4),5:3,c(100,20,90),11:13)

R> mymat

[,1] [,2] [,3]


[1] 1 3 4
[2] 5 4 3
[3] 100 20 90
[4] 11 12 13
R> dim(mymat)
[1] 43
R> nrow(mymat)
[1] 4
R> ncol(mymat)
[1] 3
R> dim(mymat)[2]
[1] 3

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

[,1] [,2] [,3]

[1,] 0.3 91.0 -4.2

[2,] 4.5 0.1 8.2

[3,] 55.3 105.5 27.9

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

As expected, you’re given the element at position [3,2].


Row, Column, and Diagonal Extractions
To extract an entire row or column from a matrix, you simply specify the
desired row or column number and leave the other value blank. It’s important
to note that you must still include the comma that separates the row and column
numbers—this is how R distinguishes between a request for a row and a request
for a column. The following returns the second column of A:

R> A[,2]
91.0 0.1 105.5

The following examines the first row:

R> A[1,]

0.3 91.0 -4.2

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)

0.3 0.1 27.9


This returns a vector with the elements along the diagonal of A, starting at
A[1,1].

Omitting and Overwriting

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

[2,] 4.5 8.2

[3,] 55.3 27.9

To overwrite particular elements, or entire rows or columns, you identify the


elements to be replaced and then assign the new values. The new elements can
be a single value, a vector of the same length as the number of elements to be
replaced, or a vector whose length evenly divides the number of elements to be
replaced. To illustrate this, let’s first create a copy of A and call it B.

R> B<-A

R> B

[,1] [,2] [,3]

[1,] 0.3 91.0 -4.2

[2,] 4.5 0.1 8.2

[3,] 55.3 105.5 27.9

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

[2,] 1.0 2.0 3.0

[3,] 55.3 105.5 27.9

The following overwrites the second column elements of the first and third
rows with 900:

R> B[c(1,3),2] <- 900

R> B

[,1] [,2] [,3]

[1,] 0.3 900 -4.2

[2,] 1.0 2 3.0

[3,] 55.3 900 27.9


Matrix Operations and Algebra
You can think of matrices in R from two perspectives. First, you can use these
structures purely as a computational tool in programming to store and operate
on results, as you’ve seen so far. Alternatively, you can use matrices for their
mathematical properties in relevant calculations, such as the use of matrix
multiplication for expressing regression model equations. This distinction is
important because the mathematical behavior of matrices is not always the same
as the more generic data handling behavior. Some of the most common
mathematical operations involving matrices, and the corresponding
functionality in R.
Matrix Transpose

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

If you “transpose the transpose” of A, you’ll recover the original matrix.


R> t(t(A))

[,1] [,2] [,3]

[1,] 2 5 2

[2,] 6 1 4

Identity Matrix

The identity matrix written as Im is a particular kind of matrix used in mathe-


matics. It’s a square m*m matrix with ones on the diagonal and zeros
elsewhere.You can create an identity matrix of any dimension using the
standard matrix function, but there’s a quicker approach using diag.

R> A <- diag(x=3)

R> A

[,1] [,2] [,3]


[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
Here you see diag can be used to easily produce an identity matrix. To clarify,
the behavior of diag depends on what you supply to it as its argument x. If, as
earlier, x is a matrix, diag will retrieve the diagonal elements of the matrix. If
x is a single positive integer, as is the case here, then diag will produce the
identity matrix of the corresponding dimension. You can find more uses of
diag on its help page.

Scalar Multiple of a Matrix

A scalar value is just a single, univariate value. Multiplication of any matrix A


by a scalar value a results in a matrix in which every individual element is
multiplied by a.
R will perform this multiplication in an element-wise manner, as you might
expect. Scalar multiplication of a matrix is carried out using the standard
arithmetic * operator.

R> A <- rbind(c(2,5,2),c(6,1,4))

R> a <- 2
R> a*A

[,1] [,2] [,3]

[1,] 4 10 4

[2,] 12 2 8
Matrix Addition and Subtraction

Addition or subtraction of two matrices of equal size is also performed in an


element-wise fashion. Corresponding elements are added or subtracted from
one another, depending on the operation.

You can add or subtract any two equally sized matrices with the standard
and - symbols.

R> A <- cbind(c(2,5,2),c(6,1,4))

R> A
[,1] [,2]
[1,] 2 6
[2,] 5 1
[3,] 2 4

R> B <- cbind(c(-2,3,6),c(8.1,8.2,-9.8))

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

In order to multiply two matrices A and B of size m n and p q, it must be true


that n = p. The resulting matrix A B will have the size m q. The elements of the
product are computed in a row-by-column fashion, where the value at position
( AB)i , j is computed by element-wise multiplication of the entries in row i of A
by the entries in column j of B, summing the result.

Unlike addition, subtraction, and scalar multiplication, matrix multiplication is


not a simple element-wise calculation, and the standard * operator cannot be
used. Instead, you must use R’s matrix product operator, written with percent
symbols as %*%. Before you try this operator, let’s first store the two example
matrices and check to make sure the number of columns in the first matrix
matches the number of rows in the second matrix using dim.

R> A <- rbind(c(2,5,2),c(6,1,4))

R> dim(A)

[1]2 3

R> B <- cbind(c(3,-1,1),c(-3,1,5))


R> dim(B)

[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] [,2] [,3]

[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

[,1] [,2] [,3] [,4]


[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

,,2

[,1] [,2] [,3] [,4]

[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):

R> BR <- array(data=rep(1:24,times=3),dim=c(3,4,2,3))


R> BR

,,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

[,1] [,2] [,3] [,4]


[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

,,2,3

[,1] [,2] [,3] [,4]

[1,] 13 16 19 22

[2,] 14 17 20 23

[3,] 15 18 21 24

You might also like