Stage 01 · Lesson 06/12

Math for Robotics · Lesson 06

Matrices

Est. 30–40 min

Why

Why robots pack linear maps into tables of numbers

Concept

The big idea: a matrix is a machine that eats a vector and returns a vector

  • A 2×2 matrix has two rows and two columns.
  • Matrix × vector: each output component is a dot product of one row with the vector.
  • Matrix × matrix: compose two maps — apply B first, then A, when you write A·B (column-vector convention).
  • Identity I leaves vectors unchanged. Rotation matrices are special: they preserve length.

How it works

How it actually works, step by step

  1. Write the table

    Rows = output recipes. Columns = how much each input component contributes.

  2. Multiply by a vector

    Outₓ = row0 · v, Outᵧ = row1 · v.

  3. Multiply two matrices

    Column j of the result is A times column j of B.

  4. Check a rotation

    For R(θ) = [[c,-s],[s,c]], |R v| should equal |v|.

Visual

Visualise it: rotation as a 2×2 matrix

Change θ and watch the basis vectors move. That motion is exactly multiplication by a 2×2 rotation matrix.

Interactive · 2D rotation visualizer

R(θ) =

[ 0.819 , -0.574 ]

[ 0.574 , 0.819 ]

R·v = (62.3, 68.0)

Length is preserved under pure rotation (|v| ≈ |R·v|). This 2×2 matrix is the planar cousin of SO(3) rotations used on 6DOF wrists.

Worked example

Worked example: B rotates (1,0) to (0,1)

Code lab

Code lab: 2×2 multiply

matrices_2x2.py · Python
"""Stage 1 L06 — multiply two 2×2 matrices and apply to a vector."""
A = [[1, 2], [3, 4]]
# R(90° CCW) = [[0,-1],[1,0]] maps (1,0) → (0,1)
B = [[0, -1], [1, 0]]
v = [1, 0]

def mat_mul(X, Y):
    return [
        [X[0][0]*Y[0][0] + X[0][1]*Y[1][0], X[0][0]*Y[0][1] + X[0][1]*Y[1][1]],
        [X[1][0]*Y[0][0] + X[1][1]*Y[1][0], X[1][0]*Y[0][1] + X[1][1]*Y[1][1]],
    ]

def mat_vec(M, u):
    return [M[0][0]*u[0] + M[0][1]*u[1], M[1][0]*u[0] + M[1][1]*u[1]]

C = mat_mul(A, B)
Bv = mat_vec(B, v)
print("A =", A)
print("B =", B)
print("A·B =", C)
print("B · [1,0] =", Bv, "  # 90° CCW: +x maps to +y")

Expected output

A = [[1, 2], [3, 4]]
B = [[0, -1], [1, 0]]
A·B = [[2, -1], [4, -3]]
B · [1,0] = [0, 1]   # 90° CCW: +x maps to +y

Real world

Where small matrices appear

  • 2×2 and 3×3 rotation blocks inside larger transforms
  • Camera intrinsics as a 3×3 matrix (later stages)
  • Jacobian matrices map joint rates → tip rates (Stage 8)

Mistakes

Common mistakes and confusions

Hardware & safety

Hardware lab and safety notes

No new parts — use your paper arm and graph paper. A rotation matrix is just a recipe that tells each point where to go when the whole arm turns.

  • Mark the tip of link 1 at (10, 0) cm. Apply B = [[0, −1], [1, 0]] by hand: new tip (0, 10).
  • Now physically turn the paper arm 90° anticlockwise about the pin. Does the tip land on (0, 10)?
  • Try A = [[2, 0], [0, 1]] (stretch x): not a rotation — your cardboard can't do it, because a rotation keeps lengths.

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
A·B ≠ B·A and you think the code is brokenMatrix multiplication is not commutativeThat is normal — keep the order carefully
Result vector has the wrong shapeRow vector used where a column is expectedUse column vectors: M · [x, y]ᵀ
Rotation also changes lengthSign slip in [[c, −s], [s, c]]Check det = 1 and the columns have length 1
NumPy gives an element-wise resultUsed * instead of @Use A @ B or np.matmul
NoteChange one thing at a time. Most Stage 1 bugs are units (degrees vs radians, mm vs m), order of multiplication, or a frame that was never named.

Think

Think about it

Think

What does the identity matrix do to a vector? What should R(0°) look like?

Experiment

Try it in the lab: multiply by hand

Experiment

Multiply by hand

You will need

  • Notebook
  • rotation-2d interactive

Steps

  1. Write R(90°) = [[0,−1],[1,0]].
  2. Compute R(90°)·(1,0) and R(90°)·(0,1) by hand.
  3. Confirm with the interactive that +x maps to +y.

Interactive · 2D rotation visualizer

R(θ) =

[ 0.819 , -0.574 ]

[ 0.574 , 0.819 ]

R·v = (62.3, 68.0)

Length is preserved under pure rotation (|v| ≈ |R·v|). This 2×2 matrix is the planar cousin of SO(3) rotations used on 6DOF wrists.

(1,0) → (0,1) and (0,1) → (−1,0) for +90° CCW.

Knowledge check

Check your understanding

Knowledge check

1. Matrix×vector output component is…

2. A·B equals B·A always?

3. Rotation matrices preserve…

4. R(0°) equals…

Practice quiz

Loading practice quiz…

Mini assignment

Hands-on assignment

Mini assignment

2×2 practice

Pick two 2×2 matrices with small integers. Multiply them by hand and with a 4-line Python script. Confirm they match.

Deliverables

  • Hand working
  • Script output

Do this, step by step

  1. Pick A = [[1, 2], [0, 1]] and B = [[0, −1], [1, 0]]. Compute A·B and B·A by hand.
  2. Edit matrices_2x2.py to print both products and compare with your hand working.
  3. Apply A·B and B·A to (1, 0). Draw both results on graph paper.
  4. Write one sentence: why does the order matter for a robot arm?

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: Row × column: entry (1,1) of A·B = 1·0 + 2·1 = 2.
  2. Hint 2: A·B = [[2, −1], [1, 0]], B·A = [[0, −1], [1, 2]].
  3. Hint 3: (A·B)·(1, 0) = (2, 1); (B·A)·(1, 0) = (0, 1). Different points — so “turn then shear” ≠ “shear then turn”.

Challenges (pick by level)

  • 🟢 Easy: Multiply [[2, 0], [0, 3]] by (4, 5).
  • 🟡 Medium: Show that R(90°)·R(90°) = R(180°) by multiplying.
  • 🟠 Hard: Write a 2×2 multiply without NumPy and test it against np.matmul on 100 random matrices.

Next step

Next, Lesson 7: Transformations adds a translation to the rotation. Related: matrices in control and state-space in Stage 7 — Control Systems.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Watch on Quark Lab · GATE RA + PH

EP02: Eigenvalues & Eigenvectors

Most arrows turn when a matrix acts on them, but a few only stretch: those are eigenvectors, and the stretch factors are eigenvalues (A v = λ v). Finding them with det(A − λI) = 0, the trace and determinant checks, symmetric matrices and diagonalization (PᵀAP = D), why every 3D rotation has eigenvalue 1 along its axis, the physics link to principal axes of inertia, a GATE-style solved problem (eigenvalues of A²) and a numerical (NAT) question.

All Quark Lab episodes →

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in