๐งฑ R Matrix Fundamentals: Building with Blocks!
Imagine you have a big box of LEGO blocks. Instead of just making a long line of blocks (thatโs a vector!), you can arrange them into a rectangle โ rows going across, columns going down. That rectangle of blocks is called a Matrix!
A matrix is like a spreadsheet or a chocolate bar with rows and columns. Each little square holds a number.
๐๏ธ Creating Matrices
The Magic matrix() Function
Think of matrix() as your LEGO instruction booklet. You give it:
- The blocks (your numbers)
- How many rows
- How many columns
# Create a 2x3 matrix (2 rows, 3 columns)
my_matrix <- matrix(
c(1, 2, 3, 4, 5, 6),
nrow = 2,
ncol = 3
)
# Result:
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
๐ค Wait, Why That Order?
R fills matrices by column (top to bottom, then next column). Itโs like filling a bookshelf โ fill one shelf, then move right!
Want to fill by rows instead? Use byrow = TRUE:
my_matrix <- matrix(
c(1, 2, 3, 4, 5, 6),
nrow = 2,
ncol = 3,
byrow = TRUE
)
# Result:
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
Now it reads left-to-right like a book! ๐
๐ Matrix Dimensions
How Big Is Your LEGO Rectangle?
Every matrix has a size โ how many rows and columns it has.
# Check dimensions
dim(my_matrix) # Returns: 2 3
# Just rows
nrow(my_matrix) # Returns: 2
# Just columns
ncol(my_matrix) # Returns: 3
# Total blocks
length(my_matrix) # Returns: 6
๐ Visual Diagram
graph TD A[Matrix 2x3] --> B[2 Rows] A --> C[3 Columns] A --> D[6 Total Elements]
Remember: dim() gives you rows, columns โ like saying โ2 floors, 3 rooms per floor!โ
๐ฏ Matrix Indexing
Finding Your Block!
Every block in your matrix has an address: [row, column]
Think of it like finding a seat in a theater:
- First number = which row (front to back)
- Second number = which seat (left to right)
my_matrix <- matrix(
1:9, nrow = 3, ncol = 3
)
# [,1] [,2] [,3]
# [1,] 1 4 7
# [2,] 2 5 8
# [3,] 3 6 9
# Get element at row 2, column 3
my_matrix[2, 3] # Returns: 8
# Get entire row 1
my_matrix[1, ] # Returns: 1 4 7
# Get entire column 2
my_matrix[, 2] # Returns: 4 5 6
๐ช Multiple Selections
Want several items? Use vectors!
# Rows 1 and 3, column 2
my_matrix[c(1, 3), 2] # Returns: 4 6
# Rows 1-2, columns 2-3
my_matrix[1:2, 2:3]
# [,1] [,2]
# [1,] 4 7
# [2,] 5 8
๐ง Matrix Row & Column Operations
Adding Names (Labels!)
Give your rows and columns friendly names:
sales <- matrix(
c(100, 150, 200, 250),
nrow = 2, ncol = 2
)
rownames(sales) <- c("Store A", "Store B")
colnames(sales) <- c("Jan", "Feb")
# Jan Feb
# Store A 100 200
# Store B 150 250
โ Combining Matrices
Stack rows with rbind() (row-bind):
row1 <- c(1, 2, 3)
row2 <- c(4, 5, 6)
rbind(row1, row2)
# [,1] [,2] [,3]
# row1 1 2 3
# row2 4 5 6
Stack columns with cbind() (column-bind):
col1 <- c(1, 2)
col2 <- c(3, 4)
cbind(col1, col2)
# col1 col2
# [1,] 1 3
# [2,] 2 4
๐ Row & Column Stats
m <- matrix(1:6, nrow = 2, ncol = 3)
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
rowSums(m) # Sum each row: 9 12
colSums(m) # Sum each col: 3 7 11
rowMeans(m) # Average each row: 3 4
colMeans(m) # Average each col: 1.5 3.5 5.5
โโโ๏ธโ Matrix Arithmetic
Element-by-Element Operations
Just like with vectors, math works on each block:
m <- matrix(c(1, 2, 3, 4), nrow = 2)
# [,1] [,2]
# [1,] 1 3
# [2,] 2 4
m + 10 # Add 10 to every element
# [,1] [,2]
# [1,] 11 13
# [2,] 12 14
m * 2 # Multiply every element by 2
# [,1] [,2]
# [1,] 2 6
# [2,] 4 8
๐ค Matrix + Matrix
When two matrices are the same size, operations happen block-by-block:
a <- matrix(c(1, 2, 3, 4), nrow = 2)
b <- matrix(c(5, 6, 7, 8), nrow = 2)
a + b # Add matching positions
# [,1] [,2]
# [1,] 6 10
# [2,] 8 12
a * b # Multiply matching positions
# [,1] [,2]
# [1,] 5 21
# [2,] 12 32
๐งฎ True Matrix Multiplication
For real matrix math (linear algebra), use %*%:
a <- matrix(c(1, 2, 3, 4), nrow = 2)
b <- matrix(c(5, 6, 7, 8), nrow = 2)
a %*% b # Matrix multiplication
# [,1] [,2]
# [1,] 23 31
# [2,] 34 46
Rule: For A %*% B, columns of A must equal rows of B!
๐ Quick Summary
| Task | Code | Example |
|---|---|---|
| Create | matrix(data, nrow, ncol) |
matrix(1:6, 2, 3) |
| Size | dim(), nrow(), ncol() |
dim(m) โ 2, 3 |
| Get element | m[row, col] |
m[2, 3] |
| Get row | m[row, ] |
m[1, ] |
| Get column | m[, col] |
m[, 2] |
| Stack rows | rbind() |
rbind(r1, r2) |
| Stack cols | cbind() |
cbind(c1, c2) |
| Row sums | rowSums() |
rowSums(m) |
| Math | +, -, *, / |
m * 2 |
| Matrix mult | %*% |
a %*% b |
๐ก You Did It!
You now know how to:
- โ Build matrices from scratch
- โ Check their size
- โ Find any element by its address
- โ Work with rows and columns
- โ Do math with matrices
Matrices are everywhere โ images, spreadsheets, games, science. You just learned a superpower! ๐ฆธโโ๏ธ
๐ Pro tip: Think of a matrix as a table of numbers โ rows are like lines in a poem, columns are like different chapters. Together, they tell a complete story!