Matrix Algebra (%*%, solve, t)

Matrix algebra in R is the set of tools — matrix multiplication with %*%, transpose with t(), and solving systems with solve() — that let you do real linear algebra on two-dimensional grids of numbers.

Learn Matrix Algebra (%*%, solve, t) in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll build matrices, multiply them the right way, transpose and invert them, and solve a system of linear equations in a single call.

What You'll Learn in This Lesson

1️⃣ Building Matrices

Use matrix() with a vector of values plus nrow and/or ncol . By default R fills down the columns ; add byrow = TRUE to fill across rows the way you'd read them. The dim() , nrow() , and ncol() helpers report the shape.

2️⃣ Multiplying and Transposing

The operator %*% performs true matrix multiplication, where each output cell is a row-times-column dot product. A plain * multiplies matching cells instead — useful, but a different operation. The transpose t() flips rows and columns.

Whenever you see a dimension-mismatch error from %*% , check that the inner dimensions agree: an m×n times an n×p gives an m×p result.

3️⃣ Inverses and Solving Systems

The single function solve() does double duty. Called with one matrix it returns the inverse; called as solve(A, b) it solves the linear system A x = b for the unknown vector x . Use det() to check invertibility and diag() to build an identity matrix.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Two equations, two unknowns — exactly what solve(A, b) is built for. Remember to use byrow = TRUE so your coefficient matrix matches the way you wrote the equations.

📋 Quick Reference — Matrix Algebra

Practice quiz

What does the %*% operator do?

  • Element-wise multiplication
  • True matrix multiplication
  • Division
  • Transposition

Answer: True matrix multiplication. %*% performs linear-algebra matrix multiplication (row-times-column dot products).

What does a plain * do between two matrices?

  • Matrix product
  • Solves a system
  • Inverts them
  • Element-by-element multiplication

Answer: Element-by-element multiplication. A single * multiplies matching cells (Hadamard), requiring equal shapes.

For A %*% B to work, what must be true?

  • A's columns equal B's rows
  • A and B are identical
  • Both must be square
  • A's rows equal B's rows

Answer: A's columns equal B's rows. The inner dimensions must match: ncol(A) must equal nrow(B).

What does t(A) compute?

  • The total
  • The determinant
  • The transpose
  • The inverse

Answer: The transpose. t() transposes A, turning rows into columns.

What does solve(A) with one argument return?

  • The inverse of A
  • The determinant
  • The transpose
  • The diagonal

Answer: The inverse of A. solve(A) returns the matrix inverse of A.

Which is the preferred way to solve the system A x = b?

  • solve(A) %*% b
  • A %*% b
  • solve(A, b)
  • det(A) * b

Answer: solve(A, b). solve(A, b) is faster and more numerically stable than inverting A first.

What does det(A) tell you?

  • The matrix size
  • Whether A is invertible (non-zero means yes)
  • The number of rows
  • The transpose

Answer: Whether A is invertible (non-zero means yes). A non-zero determinant means the matrix is invertible.

How does R fill matrix(1:6, nrow = 2) by default?

  • Row by row
  • Randomly
  • Diagonally
  • Down the columns

Answer: Down the columns. R is column-major: 1 and 2 fill the first column, then 3 and 4, and so on.

What does diag(2) produce?

  • A 2x2 identity matrix
  • The number 2
  • A diagonal of twos
  • A 2-row vector

Answer: A 2x2 identity matrix. diag(n) builds an n-by-n identity matrix.

What does 'system is computationally singular' mean?

  • The matrix is too large
  • There are too many rows
  • The matrix has no inverse (determinant ~ 0)
  • b is the wrong length

Answer: The matrix has no inverse (determinant ~ 0). It signals a non-invertible matrix, so the system has no unique solution.