A matrix is a collection of data elements of the same type arranged in a two-dimensional rectangle
To create a matrix we must indicate the elements, as well as the number of rows (nrow) and columns (ncol)
To declare a matrix in R use the function mat () and a name. As a rule of thumb, matrix names are capitalized. However, R takes even lower case.
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
To check the components of the mat () function use;
By default, any matrix is created column-wise. To change that we set an additional argumnet byrow = TRUE.
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
It is not necessary to specify both the number of rows and columns We can only indicate one of them. The number of elements must be a multiple of the number of rows or columns
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
To get the class use the fucntion class()
## [1] "matrix"
Other functions include
dim() dimesnion of matrix nrow total rows ncol total columns
example;
## [1] 3 3
## [1] 3
## [1] 3
To check is somethin is a matrix use;
## [1] TRUE
rbind() and cbind() allow us to bind vectors in order to create a matrix. The vectors must have the same length
Example;
Declare three vectors.
If we use rbind(), our vectors will be rows
## [,1] [,2] [,3] [,4]
## a 1 2 3 4
## b 10 11 12 13
## c 20 30 40 50
The result is a matrix.
## [1] "matrix"
The order does not matter.
## [,1] [,2] [,3] [,4]
## c 20 30 40 50
## a 1 2 3 4
## b 10 11 12 13
Vectors can be repeated.
## [,1] [,2] [,3] [,4]
## a 1 2 3 4
## b 10 11 12 13
## c 20 30 40 50
## a 1 2 3 4
It is not necessary to create the vectors first. We can enter them directly in the rbind() function.
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 7 8 9
## [3,] 2 3 4
If we use cbind() the vectors will be columns.
## a b c
## [1,] 1 10 20
## [2,] 2 11 30
## [3,] 3 12 40
## [4,] 4 13 50
Naming with dimnames()
e <- matrix(c(1,2,3,4,5,6), nrow = 2,
dimnames = list(c("row1", "row2"), c("col1", "col2", "col3")))
e## col1 col2 col3
## row1 1 3 5
## row2 2 4 6
Using the functions rownames() and colnames()
e <- matrix(c(1,2,3,4,5,6), nrow = 2)
rownames(e) <- c("row1", "row2")
colnames(e) <- c("col1", "col2", "col3")
e## col1 col2 col3
## row1 1 3 5
## row2 2 4 6
Remove row and column names, assign NULL.
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
Indexing means accessing one or several matrix elements. Indices must be put between square brackets We must use two indices: one for the row and the other one for the column
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## [4,] 13 14 15 16
Access the element on row 3, column 2
## [1] 10
Access the element on row 4, column 1
## [1] 13
## [,1] [,2] [,3]
## [1,] 10 40 70
## [2,] 20 50 80
## [3,] 30 60 90
## [,1] [,2] [,3]
## [1,] 2 8 14
## [2,] 4 10 16
## [3,] 6 12 18
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
Combining matrices;
Rowise combination.
## [,1] [,2] [,3]
## [1,] 2 -1 10
## [2,] 4 2 11
## [3,] 6 -1 12
## [4,] 1 4 7
## [5,] 2 5 8
## [6,] 3 6 9
Columnwise combination.
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2 -1 10 1 4 7
## [2,] 4 2 11 2 5 8
## [3,] 6 -1 12 3 6 9
Row and column sums.
## [1] 12 0 33
## [1] 11 17 17
Row and column means.
## [1] 4 0 11
## [1] 3.666667 5.666667 5.666667
Some types of matrices.
Identity matrix
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
Unity matrix
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
## [3,] 1 1
Install and load matlib R package.
Example
## -1*x1 - 1*x2 = -2
## 2*x1 + 2*x2 = 1
## -x[1] - 1*x[2] = -2
## 2*x[1] + 2*x[2] = 1
## x1 + x2 = 1/2
## 0 = -3/2
Example;
Solve the equation; \(\begin{array}{ccccccc} 4x & - & 3y & + & z & = & -10\\ 2x & + & y & + & 3z & = & 0\\ -x & + & 2y & - & 5z & = & 17 \end{array}\)
## 4*x1 + 2*x2 - 1*x3 = -10
## -3*x1 + 1*x2 + 2*x3 = 0
## 1*x1 + 3*x2 - 5*x3 = 17
## x1 = -13/4
## x2 = -3/4
## x3 = -9/2
A <- matrix(c(1,-2,4,-5,2,3,6,2,4), nrow = 3, ncol = 3)
b <- c(23,45,32)
plotEqn3d (A,b)
Solve(A, b, fractions = TRUE)## x1 = -526/81
## x2 = 490/81
## x3 = 1613/162
Another example;
## 1*x1 - 1*x2 = 2
## 2*x1 + 2*x2 = 1
## 3*x1 + 1*x2 = 3
## x[1] - 1*x[2] = 2
## 2*x[1] + 2*x[2] = 1
## 3*x[1] + x[2] = 3
## x1 = 5/4
## x2 = -3/4
## 0 = 0
To perform operations on the matrix rows and columns we can use the apply() function
Create a matrix.
## [,1] [,2] [,3] [,4]
## [1,] 10 14 18 22
## [2,] 11 15 19 23
## [3,] 12 16 20 24
## [4,] 13 17 21 25
Compute the sum of the elements on each row and column, respectively
## [1] 64 68 72 76
## [1] 46 62 78 94
Compute the product of the elements on each row and column, respectively.
## [1] 55440 72105 92160 116025
## [1] 17160 57120 143640 303600
Compute the mean for each row and column, respectively
## [1] 16 17 18 19
## [1] 11.5 15.5 19.5 23.5
Compute the standard deviation for each row and column, respectively
## [1] 5.163978 5.163978 5.163978 5.163978
## [1] 1.290994 1.290994 1.290994 1.290994
Compute the cumulative sums for the data values in each row
## [,1] [,2] [,3] [,4]
## [1,] 10 11 12 13
## [2,] 24 26 28 30
## [3,] 42 45 48 51
## [4,] 64 68 72 76
The cumulative sums are computed by row, BUT the matrix is built column-wise (the default way in R)
Create a mtrix row-wise with byrow=TRUE
## [,1] [,2] [,3] [,4]
## [1,] 10 24 42 64
## [2,] 11 26 45 68
## [3,] 12 28 48 72
## [4,] 13 30 51 76