Notes on Matrices

For the course LBYMATH - Mathematical Economics (Laboratory) for Term 3, A.Y. 2025-2026

Lecture No. 1 (5/21/2026)

Setting up matrices

In order to set a matrix, the following code shall be executed:

A <- matrix(c(1,2,3,4,5,6), nrow=2, ncol=3, byrow=TRUE)

# SYNTAX:
# matrix()  : the syntax to create a matrix
# c()       : contatenate function
# nrow      : number of rows
# ncol      : number of columns
# byrow     : fills 1st rows first before moving on to the next

Column Vector

A_col <- matrix(c(1,2,3,4,5,6))
A_col
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
[6,]    6
A_col <- matrix(c(1,2,3,4,5,6), ncol=2)
A_col
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

When setting up a matrix wherein ncol is a number less than the maximum number of data

Row Vector

A_row <- matrix(c(1,2,3,4,5,6), nrow=1)
A_row
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6

General Matrix

# Method 1
A_row1 <- matrix(c(1,2,3,4,5,6), nrow=2, ncol=3, byrow=TRUE)
A_row1
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
# Method 2
A_row2 <- matrix(c(1,4,2,5,3,6), nrow=2, ncol=3)
A_row2
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

What would happen if Method 2 used c(1,2,3,4,5,6)?

To replace an entry in a matrix, you can assign using A[i,j] <- x such that i is the row number and j is the column number. When replacing an entire row or column, simply omit either i or j.

# Replacing a singular entry with a new set of data
A[1,3] <- 7
A
     [,1] [,2] [,3]
[1,]    1    2    7
[2,]    4    5    6
#A[i,j] : where i = row, j is column
# Replacing an entire row with a new set of data
A[1,] <- c(7,8,9)
A
     [,1] [,2] [,3]
[1,]    7    8    9
[2,]    4    5    6

Matrix Operations

# Setting up matrices
B <- matrix(c(1,2,3,4,5,6), ncol=2, nrow=3, byrow=TRUE)
C <- matrix(c(3,-2,7,5,0,1), ncol=2, nrow=3, byrow=TRUE)
B
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
C
     [,1] [,2]
[1,]    3   -2
[2,]    7    5
[3,]    0    1

Matrix Addition/Subtraction

Two matrices can be added only if they have the same dimensions.

D <- B+C
D
     [,1] [,2]
[1,]    4    0
[2,]   10    9
[3,]    5    7

While addition has commutative property (i.e., the order does not matter), subtraction needs to be ordered since

Scalar Multiplication

D <- 6*C
D
     [,1] [,2]
[1,]   18  -12
[2,]   42   30
[3,]    0    6

Matrix Multiplication

G <- A %*% C

Matrix Transposition

A
     [,1] [,2] [,3]
[1,]    7    8    9
[2,]    4    5    6
H <- t(A)
H
     [,1] [,2]
[1,]    7    4
[2,]    8    5
[3,]    9    6

Types of Matrices

  1. Square Matrix (m = n, s.t. n=n)
  2. Symmetric Matrix (J = J^t)
  3. Diagonal Matrix
  4. Identity Matrix
  5. Triangular Matrix
  6. Idempotent Matrix

Square Matrix

Symmetric Matrix

J <- matrix(c(-1,0,3,0,7,6,3,6,-5), nrow=3, ncol=3, byrow=TRUE)

Diagonal Matrix

In diagonal matrices, the matrix must be a square matrix wherein all data points are placed in the principal diagonal.

\[ D = \begin{bmatrix}d_{11} & 0 & \cdots & 0 \\0 & d_{22} & \cdots & 0 \\\vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & d_{nn}\end{bmatrix} \]

There are two ways to use diagonal matrices using the diag() prompt:

  1. To construct a diagonal matrix by nesting a c() function
  2. To extract entries from the main diagonal by inputting a matrix as an argument within the diag() prompt
# Construct a diagonal matrix
K <- diag(c(1,2,3))
K
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    2    0
[3,]    0    0    3
# Extract a diagonal matrix
J
     [,1] [,2] [,3]
[1,]   -1    0    3
[2,]    0    7    6
[3,]    3    6   -5
diag(J)
[1] -1  7 -5
# Replace the diagonal elements in a matrix
diag(J) <- c(1,2,3)