Matrix Operations

Addition, scalar multiplication, matrix-vector products, and dimension checks.

Matrix operations look like ordinary arithmetic, but they have one extra rule: shape matters first. Before adding or multiplying, always check the dimensions.

What you should be able to do

By the end of this lesson, you should be able to:

  • add matrices of the same shape;
  • multiply a matrix by a scalar;
  • decide whether matrix products are defined;
  • compute matrix-vector products by row dot products;
  • read a matrix-vector product as a combination of columns;
  • verify calculations with edumath, NumPy, and SymPy.

Matrix addition

You can add matrices only when they have the same shape. Add matching entries:

\[ \begin{bmatrix}1&2\\3&4\end{bmatrix} + \begin{bmatrix}5&6\\7&8\end{bmatrix} = \begin{bmatrix}6&8\\10&12\end{bmatrix}. \]

You cannot add a \(2\times 2\) matrix to a \(2\times 3\) matrix because there are entries with no matching partner.

Scalar multiplication

A scalar multiplies every entry:

\[ -2\begin{bmatrix}3&-1\\0&4\end{bmatrix} = \begin{bmatrix}-6&2\\0&-8\end{bmatrix}. \]

This is similar to scalar multiplication for vectors.

Matrix-vector multiplication

A matrix-vector product \(A\mathbf{x}\) is defined when the number of columns in \(A\) equals the number of entries in \(\mathbf{x}\).

If

\[ A=\begin{bmatrix} 2 & 1\\ 0 & 3 \end{bmatrix} \quad \text{and} \quad \mathbf{x}=\begin{bmatrix}4\\5\end{bmatrix}, \]

then

\[ A\mathbf{x} = \begin{bmatrix} 2\cdot4+1\cdot5\\ 0\cdot4+3\cdot5 \end{bmatrix} = \begin{bmatrix}13\\15\end{bmatrix}. \]

Each output entry is a row dot product.

Column-combination view

The same product can be read another way:

\[ \begin{bmatrix} 2 & 1\\ 0 & 3 \end{bmatrix} \begin{bmatrix}4\\5\end{bmatrix} =4\begin{bmatrix}2\\0\end{bmatrix} +5\begin{bmatrix}1\\3\end{bmatrix}. \]

So a matrix-vector product is a linear combination of the columns of the matrix. This idea becomes very important when studying systems and transformations.

Matrix-matrix multiplication

A product \(AB\) is defined when the number of columns of \(A\) equals the number of rows of \(B\):

\[ (m\times n)(n\times p) = m\times p. \]

For example, a \(2\times3\) matrix times a \(3\times4\) matrix produces a \(2\times4\) matrix.

WarningCommon pitfall

Matrix multiplication is not entry-by-entry multiplication. It uses rows of the left matrix and columns of the right matrix. Also, \(AB\) and \(BA\) may be different or one of them may not even be defined.

Worked example: check dimensions first

Can a \(3\times2\) matrix multiply a vector with 2 entries?

  1. The matrix has 2 columns.
  2. The vector has 2 entries.
  3. The inner dimensions match, so the product is defined.
  4. The result has 3 entries because the matrix has 3 rows.

Can a \(3\times2\) matrix multiply a vector with 3 entries? No. The matrix has 2 columns, but the vector has 3 entries.

Practice exercises

  1. Add \(\begin{bmatrix}1&0\\2&3\end{bmatrix}+\begin{bmatrix}4&5\\6&7\end{bmatrix}\).
  2. Compute \(3\begin{bmatrix}2&-1\\0&4\end{bmatrix}\).
  3. Is \((2\times3)(3\times1)\) defined? What is the output shape?
  4. Compute \(\begin{bmatrix}1&2\\3&4\end{bmatrix}\begin{bmatrix}5\\6\end{bmatrix}\).
  5. Is \((2\times3)(2\times1)\) defined?
  1. \(\begin{bmatrix}5&5\\8&10\end{bmatrix}\).
  2. \(\begin{bmatrix}6&-3\\0&12\end{bmatrix}\).
  3. Yes. The output shape is \(2\times1\), a vector with 2 entries.
  4. \(\begin{bmatrix}17\\39\end{bmatrix}\).
  5. No. The inner dimensions are 3 and 2, and they do not match.

Subtopic guided practice and checkpoints

For matrix operations, the safest order is: check shape, choose the operation, then calculate slowly.

Lab 1: decide whether addition is allowed

Guess first. Can you add a \(2\times3\) matrix and a \(3\times2\) matrix?

Guided exercise.

Matrix addition needs identical shapes. For

\[ \begin{bmatrix}1&2\\3&4\end{bmatrix} + \begin{bmatrix}-1&5\\0&6\end{bmatrix}, \]

both matrices are \(2\times2\), so addition is allowed. Add matching entries:

\[ \begin{bmatrix}1+(-1)&2+5\\3+0&4+6\end{bmatrix} = \begin{bmatrix}0&7\\3&10\end{bmatrix}. \]

Checkpoint. Decide whether \(\begin{bmatrix}1&2&3\\4&5&6\end{bmatrix}\) can be added to \(\begin{bmatrix}1&2\\3&4\\5&6\end{bmatrix}\). Explain using shape.

Lab 2: multiply every entry by a scalar

Guess first. If a matrix has one negative entry, can scalar multiplication make it positive?

Guided exercise.

Compute

\[ -2\begin{bmatrix}4&-3\\0&5\end{bmatrix} = \begin{bmatrix}-8&6\\0&-10\end{bmatrix}. \]

The scalar touches every entry, including zeros.

Checkpoint. Compute \(3\begin{bmatrix}-1&2\\4&0\end{bmatrix}\).

Lab 3: multiply a matrix by a vector using row dot products

Guess first. If a \(3\times2\) matrix multiplies a 2-entry vector, how many entries should the answer have?

Guided exercise.

Let

\[ A=\begin{bmatrix}1&2\\3&0\\-1&4\end{bmatrix}, \qquad \mathbf{x}=\begin{bmatrix}5\\2\end{bmatrix}. \]

The product is defined because \(A\) has 2 columns and \(\mathbf{x}\) has 2 entries. Now use one row at a time:

\[ A\mathbf{x}= \begin{bmatrix} 1(5)+2(2)\\ 3(5)+0(2)\\ -1(5)+4(2) \end{bmatrix} = \begin{bmatrix}9\\15\\3\end{bmatrix}. \]

Checkpoint. Compute \(\begin{bmatrix}2&-1\\0&3\end{bmatrix}\begin{bmatrix}4\\5\end{bmatrix}\).

Lab 4: read a product as a column combination

Guess first. In \(A\mathbf{x}\), do the entries of \(\mathbf{x}\) multiply rows or columns in the column-combination view?

Guided exercise.

For

\[ A=\begin{bmatrix}2&1\\0&3\end{bmatrix}, \qquad \mathbf{x}=\begin{bmatrix}4\\5\end{bmatrix}, \]

use the entries of \(\mathbf{x}\) as column weights:

\[ A\mathbf{x}=4\begin{bmatrix}2\\0\end{bmatrix} +5\begin{bmatrix}1\\3\end{bmatrix} =\begin{bmatrix}8\\0\end{bmatrix}+\begin{bmatrix}5\\15\end{bmatrix} =\begin{bmatrix}13\\15\end{bmatrix}. \]

Checkpoint. Write \(\begin{bmatrix}1&3\\2&-1\end{bmatrix}\begin{bmatrix}2\\4\end{bmatrix}\) as a combination of the two columns, then compute it.

Lab 5: check matrix-matrix dimensions before multiplying

Guess first. What is the output shape of a \((4\times2)(2\times5)\) product?

Guided exercise.

Use the dimension pattern:

\[ (m\times n)(n\times p)=m\times p. \]

So

\[ (4\times2)(2\times5)=4\times5. \]

The inner dimensions match and disappear from the output shape.

Checkpoint. Decide whether \((3\times4)(2\times3)\) is defined. If not, state which dimensions fail to match.

  1. No. The first matrix is \(2\times3\) and the second is \(3\times2\).
  2. \(3\begin{bmatrix}-1&2\\4&0\end{bmatrix}=\begin{bmatrix}-3&6\\12&0\end{bmatrix}\).
  3. The product is \(\begin{bmatrix}2(4)+(-1)(5)\\0(4)+3(5)\end{bmatrix}= \begin{bmatrix}3\\15\end{bmatrix}\).
  4. \(2\begin{bmatrix}1\\2\end{bmatrix}+4\begin{bmatrix}3\\-1\end{bmatrix} =\begin{bmatrix}14\\0\end{bmatrix}\).
  5. \((3\times4)(2\times3)\) is not defined because the inner dimensions \(4\) and \(2\) do not match.

Matrix-operation guessing game

Use this game to practice dimension checks and matrix-vector products.

Using this lesson with edumath and SymPy

NumPy is the standard numeric array library in Python. SymPy is better when you want exact symbolic or fractional answers. The edumath helpers keep examples short for lessons.

from edumath.linear_algebra import can_multiply, matrix_product, matrix_vector_product

A = [[2, 1], [0, 3]]
x = [4, 5]

print(can_multiply((2, 2), (2,)))
print(matrix_vector_product(A, x))
print(matrix_product([[1, 2]], [[3], [4]]))
True
[13. 15.]
[[11.]]
import sympy as sp

A = sp.Matrix([[2, 1], [0, 3]])
x = sp.Matrix([4, 5])
A * x

\(\displaystyle \left[\begin{matrix}13\\15\end{matrix}\right]\)