Name: Achmad Fahry Baihaki
NIM: 2206065110100
Institute: Maulana Malik Ibrahim Islamic State University of Malang
Departement: Computer Science
Lecturer: Prof. Dr. Suhartono, M.Kom


What are Matrices?

Matrices, the plural form of a matrix, are the arrangements of numbers, variables, symbols, or expressions in a rectangular table that contains various numbers of rows and columns. They are rectangular-shaped arrays, for which different operations like addition, multiplication, and transposition are defined. The numbers or entries in the matrix are known as its elements. Horizontal entries of matrices are called rows and vertical entries are known as columns.

In every programming languages, matrix is used to store data table. Such a big of data.

Creating a Matrix

To creates matrices in R programming language, we can use cbind() or rbind() function. Here’s the example:

# Creates Numeric Vector
col1 <- c(5, 6, 7, 8, 9)
col2 <- c(2, 4, 5, 9, 8)
col3 <- c(7, 3, 4, 8, 7)

# Merge Vector based on columns
my_data <- cbind(col1, col2, col3)
my_data
##      col1 col2 col3
## [1,]    5    2    7
## [2,]    6    4    3
## [3,]    7    5    4
## [4,]    8    9    8
## [5,]    9    8    7

We can add row names using rownames() function.

# Change or Add Row Names
rownames(my_data) <- c("row1", "row2", 
                       "row3", "row4", 
                       "row5")
my_data
##      col1 col2 col3
## row1    5    2    7
## row2    6    4    3
## row3    7    5    4
## row4    8    9    8
## row5    9    8    7
Notes:
  • cbind(): merge objects/variables based on columns.
  • rbind(): merge objects/variables based on rows.
  • rownames(): get and set row names from object like matrices
  • colnames(): get and set column names from object like matrices

We can transpose (rotate the matrix so that the columns become rows and vice versa) using the t() function. Here’s an example of its implementation:

t(my_data)
##      row1 row2 row3 row4 row5
## col1    5    6    7    8    9
## col2    2    4    5    9    8
## col3    7    3    4    8    7

Creating a Matrix Using matrix() Function

These are the syntax:

# matrix(matrices = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
Notes:
  • matrices: object name for matrix data
  • nrow, ncol: desired number of rows and columns respectively.
  • byrow: logical value. If FALSE (default) the matrix is filled by columns, otherwise, the matrix is filled by rows.
  • dimnames: A list of two objects/variables giving the row and column names respectively.

In the R code below, the input data has length 8. We want to create a matrix with 4 columns. We don’t need to specify the number of rows (here nrow = 4). R will infer this automatically. The matrix is filled column by column when the byrow argument = FALSE. If we want to fill the matrix with rows, use byrow = TRUE. The following is an example of creating a matrix using the matrix() function.

datas <- matrix(
           data = c(10, 9, 8, 7,  20, 90, 50, 90), 
           nrow = 2, byrow = TRUE,
           dimnames = list(c("row1", "row2"), 
                           c("col1", "col2", "col3", "col4"))
         )
datas
##      col1 col2 col3 col4
## row1   10    9    8    7
## row2   20   90   50   90

To find out the dimensions of a matrix, we can use the ncol() function to find out the number of matrix columns and nrow() to find out the number of rows in the matrix. Here’s an example of its implementation:

# Amount of columns
ncol(datas)
## [1] 4
# Amount of rows
nrow(datas)
## [1] 2

If we want to get a summary regarding the dimensions of the matrix, we can also use the dim() function to find out the number of rows and columns of the matrix. Here’s an example of its implementation:

# Matrix order
dim(datas)
## [1] 2 4