Matrix

Matrix is a rectangular arrangement of numbers in rows and columns.

In a matrix, rows run horizontally and columns run vertically.

Creating Matrices

1. General Way To Create A Matrix

A = matrix( c(1,2,3,4,5,6,7,8,9),
            nrow = 3,
            ncol = 3,
            byrow = TRUE )
print(A)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
  • The first argument provides the input for matrix.
  • The second argument provides the number of rows.
  • The third argument provides the number of columns.
  • byrow = TRUE means the elemnts of matrix will be filled row-wise.
  • By default, byrow = FALSE, so the elements are arranged column-wise first.

2. Matrix with Single Constant Value

If we have to create a matrix where all the rows and columns are to be filled with a single constant element then we can use the following code :

B = matrix(3,4,5)
print(B)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    3    3    3    3    3
## [2,]    3    3    3    3    3
## [3,]    3    3    3    3    3
## [4,]    3    3    3    3    3

3. Diagonal Matrix

In a diagonal matrix; except the primary diagonal, all other values are null.

We can create a diagonal matrix as follows :

k = c(4,5,6)
C = diag(k,3,3)
print(C)
##      [,1] [,2] [,3]
## [1,]    4    0    0
## [2,]    0    5    0
## [3,]    0    0    6

4. Identity Matrix

An identity matrix is a diaagonal matrix with all the diagonal element as 1.

I = diag(1,3,3)
print(I)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Exploring A Matrix

To find the size of a matrix :

dim(A)
## [1] 3 3

To find the number of rows in a matrix :

nrow(A)
## [1] 3

To find the number of columns in a matrix :

ncol(A)
## [1] 3

To find the length of the matrix, i.e., total number of elements in a matrix :

length(A)

or,

prod(dim(A))
## [1] 9

Accessing Matrix Elements

Strings can be assigned as the names of rows and columns using rownames() and colnames() functions.

Let’s create a \((3\times3)\) matrix \(M\) with appropriate row and columns names :

elements = c(10,20,30,40,50,60,70,80,90)

M = matrix(elements, 3, 3, byrow = T)
colnames(M) = c("a", "b", "c")
rownames(M) = c("d","e", "f")

print(M)
##    a  b  c
## d 10 20 30
## e 40 50 60
## f 70 80 90

To access the 1st and 2nd column of the matrix :

M[,1:2]
##    a  b
## d 10 20
## e 40 50
## f 70 80

To access the 1st and 3rd columns using the assigned column names :

M[,c("a", "c")]
##    a  c
## d 10 30
## e 40 60
## f 70 90

To access the 1st and 3rd rows using the assigned row names :

M[c("d","f"),]
##    a  b  c
## d 10 20 30
## f 70 80 90

To access the element of 2nd row and 3rd column :

M[2,3]
## [1] 60

To get all the elements of the 1st column :

M[,1]
##  d  e  f 
## 10 40 70

To get all the elements of the 2nd row :

M[2,]
##  a  b  c 
## 40 50 60

To access all the elements of the matrix except the 2nd column :

M[,-2]
##    a  c
## d 10 30
## e 40 60
## f 70 90

To access all the elements of the matrix except the 2nd row :

M[-2,]
##    a  b  c
## d 10 20 30
## f 70 80 90

Colon Operator & Sub-Matrix

Colon operator can be used to create a row matrix :

1:10 #List of numbers from 1 to 10
##  [1]  1  2  3  4  5  6  7  8  9 10
10:1 #List of numbers from 10 to 1
##  [1] 10  9  8  7  6  5  4  3  2  1

Create a sub-matrix from matrix \(M\) that has first 3 rows and first two columns :

M[1:3,1:2]

Or,

M[1:3,-3]

Or,

M[,1:2]
##    a  b
## d 10 20
## e 40 50
## f 70 80

Create a sub-matrix from matrix \(M\) that has 1st and 3rd row elements of 1st 2 columns :

M[c(1,3),1:2]

or,

M[c(1,3),c(1,2)]
##    a  b
## d 10 20
## f 70 80

Matrix Concatenation

Matrix concatenation refers to merging of row or, column to a matrix.

  • Concatenation of a row to a matix is done using rbind().
  • Concatenation of a column to a matrix is done using cbind().

Consistency of the dimensions between the matrix and the vectors should be checked before concatenation.

Let’s add an additional row to the matrix \(A\) :

print(A)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
X = matrix(c(10,11,12),1,3)
print(X)
##      [,1] [,2] [,3]
## [1,]   10   11   12
Y = rbind(A,X)
print(Y)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12

Similarly, lets add an additional column to matrix \(A\)

P = matrix(c(30,60,90),3,1)
print(P)
##      [,1]
## [1,]   30
## [2,]   60
## [3,]   90
Q = cbind(A,P)
print(Q)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3   30
## [2,]    4    5    6   60
## [3,]    7    8    9   90

Matrix Algebra

In Matrix algebra, we have

  • Matrix Addition
  • Matrix Subtraction
  • Matrix Multiplication (Regular)
  • Matrix Multiplication (Elementwise)
  • Matrix Division (Elementwise)

Let’s create 2 sample matrix of \((3\times 3)\) order to perform such operations :

mat1 = matrix(c(1,2,3,4,5,6,8,9,1),3,3, byrow = T)
mat2 = matrix(c(3,1,3,4,2,1,5,1,2),3,3, byrow = T)

print(mat1)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    8    9    1
print(mat2)
##      [,1] [,2] [,3]
## [1,]    3    1    3
## [2,]    4    2    1
## [3,]    5    1    2
  1. Matrix Addition :
mat1 + mat2
##      [,1] [,2] [,3]
## [1,]    4    3    6
## [2,]    8    7    7
## [3,]   13   10    3
  1. Matrix Subtraction :
mat1 - mat2
##      [,1] [,2] [,3]
## [1,]   -2    1    0
## [2,]    0    3    5
## [3,]    3    8   -1
  1. Matrix Multiplication (Regular) :
mat1 %*% mat2
##      [,1] [,2] [,3]
## [1,]   26    8   11
## [2,]   62   20   29
## [3,]   65   27   35
  1. Matrix Multiplication (Elementwise) :
mat1 * mat2
##      [,1] [,2] [,3]
## [1,]    3    2    9
## [2,]   16   10    6
## [3,]   40    9    2
  1. Matrix Division (Elementwise) :
mat1 / mat2
##           [,1] [,2] [,3]
## [1,] 0.3333333  2.0  1.0
## [2,] 1.0000000  2.5  6.0
## [3,] 1.6000000  9.0  0.5