Matrix Fundamentals

Loading concept...

๐Ÿงฑ 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:

  1. The blocks (your numbers)
  2. How many rows
  3. 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!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.