Matrices

A matrix is a two dimensional data structure that is similar to a vector but additionally contains the dimension attribute.

1. Creating

When creating a matrix, the last two arguments to matrix tell it the number of rows and columns the matrix should have.

1.1 Matrix Code Example

You can use the byrow=TRUE argument to create a matrix by rows instead of by columns as shown below:

Let’s create a matrix mymat
mymat <- matrix(1:12,4,3)
printing out matrix
mymat
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12

1.2 Matrix Code Example

Let’s use the byrow=TRUE argument to create a matrix by rows instead of by columns as shown below
mymat <- matrix(1:12,ncol=3,byrow=TRUE)

2. Naming

2.1 Matrix Naming Code Example

# In order to remember what is stored in a matrix, you can add the names of the columns and rows. This will also help you to read the data as well as select elements from the matrix.

Let’s create our matrix

kenya <- c(460.998, 314.4) 
ethiopia <- c(290.475, 247.900) 
chad <- c(309.306, 165.8)

Then create a matrix geography matrix

geography_matrix <- matrix(c(kenya, ethiopia, chad), nrow = 3, byrow = TRUE)
Now let’s start naming
location <- c("Lat", "Long")
countries <- c("Kenya", "Ethiopia", "Chad")
Now let’s name the columns with location
colnames(geography_matrix) <- location

Let’s name the rows with countries

rownames(geography_matrix) <- countries
Now let’s print final product
geography_matrix
##              Lat  Long
## Kenya    460.998 314.4
## Ethiopia 290.475 247.9
## Chad     309.306 165.8

Now let’s create a matrix family with column names Name, Age, Gender and Occupation.

Matriachy <- c("Matriachy", 60, "F", "Queen")
Ryan <- c("Ryan", 7, "M", "Student")
Dynasty <- c("Dynasty", 30, "F", "Scientist")
Migan <- c("Migan", 26, "F", "Chef")
Wolfcode <- c("Wolfcode", 19, "M", "Engineer")
family <- matrix(c(Matriachy, Ryan, Dynasty, Migan, Wolfcode), nrow = 5, byrow = TRUE)
Header <- c("Name", "Age", "Gender", "Occupation")
Name <- c("Matriachy", "Ryan", "Dynasty", "Migan", "Wolfcode")
colnames(family) <- Header
rownames(family) <- Name
family
##           Name        Age  Gender Occupation 
## Matriachy "Matriachy" "60" "F"    "Queen"    
## Ryan      "Ryan"      "7"  "M"    "Student"  
## Dynasty   "Dynasty"   "30" "F"    "Scientist"
## Migan     "Migan"     "26" "F"    "Chef"     
## Wolfcode  "Wolfcode"  "19" "M"    "Engineer"

3. Adding a Column

You can add a row to a matrix using the rbind() function.

3.1 Adding a Column Code Example

let’s create an x matrix

x <- matrix(1:9, nrow = 3)

Then ass a column

cbind(x, c(1, 2, 3))
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7    1
## [2,]    2    5    8    2
## [3,]    3    6    9    3
Let’s Add a column residence to your fictional family matrix
cbind(family, c("Arabia Terra", "Amazonis Planitia", "Amazonis Planitia", "Arabia Terra", "Amazonis Planitia"))
##           Name        Age  Gender Occupation                     
## Matriachy "Matriachy" "60" "F"    "Queen"     "Arabia Terra"     
## Ryan      "Ryan"      "7"  "M"    "Student"   "Amazonis Planitia"
## Dynasty   "Dynasty"   "30" "F"    "Scientist" "Amazonis Planitia"
## Migan     "Migan"     "26" "F"    "Chef"      "Arabia Terra"     
## Wolfcode  "Wolfcode"  "19" "M"    "Engineer"  "Amazonis Planitia"

4. Adding a Row

4.1 Adding a Row Code Example

Let’s create a matrix

x <- matrix(1:9, nrow = 3)

now let’s add a row using cbind

rbind(x,c(1,2,3))
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## [4,]    1    2    3

Let’s Add a fictional character to our fictional family matrix

rbind(family, c("Ruby", 3, "F", "Youngling", "Amazonis Planitia"))
## Warning in rbind(family, c("Ruby", 3, "F", "Youngling", "Amazonis Planitia")):
## number of columns of result is not a multiple of vector length (arg 2)
##           Name        Age  Gender Occupation 
## Matriachy "Matriachy" "60" "F"    "Queen"    
## Ryan      "Ryan"      "7"  "M"    "Student"  
## Dynasty   "Dynasty"   "30" "F"    "Scientist"
## Migan     "Migan"     "26" "F"    "Chef"     
## Wolfcode  "Wolfcode"  "19" "M"    "Engineer" 
##           "Ruby"      "3"  "F"    "Youngling"

5. Selecting a Matrix

To select an element of a matrix, one needs to specify both the row and the column as shown:

Let’s first select a matrix

x <- matrix(1:9, nrow = 3)

let’s Select the elements from the above matrix

selecting the elemnt at 1st row 3rd column
x[1,3]
## [1] 7

Selecting second row

x[2, ]
## [1] 2 5 8

selecting 3rd column

x[ ,3]
## [1] 7 8 9

lets now select the last member of the family

family[5, ]
##       Name        Age     Gender Occupation 
## "Wolfcode"       "19"        "M" "Engineer"
selecting the first member of the family
family[1, ]
##        Name         Age      Gender  Occupation 
## "Matriachy"        "60"         "F"     "Queen"

6. Operations

Matrix addition & subtraction

6.1 Matrix Addition & Subtraction Code Example

Matrix addition and subtract require the matrices to have the same dimensions.

Let’s create matrices x and y
x <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
y <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)

Matrix Multiplication & Division

6.2 Matrix Multiplication & Division Code Example

Let’s create two 2 x 3 matrices x and y

x <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
y <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)

Multiply the matrices and assign it to variable z

z <- x * y

printing z

z
##      [,1] [,2] [,3]
## [1,]   15    0    6
## [2,]   18   36   24

Let also divide the matrices

z <- x/y
z
##      [,1]      [,2]      [,3]
## [1,]  0.6      -Inf 0.6666667
## [2,]  4.5 0.4444444 1.5000000