Matrix Operations & Dot Products

Matrix multiplication is a way of combining two matrices by pairing the rows of the first with the columns of the second — and in NumPy it is a completely different operation from element-wise multiplication.

Learn Matrix Operations & Dot Products in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

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

In this lesson you'll learn why * is element-wise while @ , np.dot , and np.matmul are the true matrix product, plus matrix-vector products and 1D inner products.

This is the single most important distinction in this lesson. The * operator multiplies matching positions together — that's element-wise multiplication. The @ operator performs matrix multiplication , combining whole rows and columns. They almost always give different answers.

Expected output: element-wise [[5 12] [21 32]] , then the matrix product [[19 22] [43 50]] printed three identical times (top-left is 1*5 + 2*7 = 19 ). Shape rule: an (m, n) times an (n, p) matrix gives an (m, p) result.

Multiplying a matrix by a vector transforms that vector. An (m, n) matrix times an (n,) vector produces an (m,) vector. Each entry of the result is the dot product of a row with the vector.

Expected output: [10 10 15] — each value is one row of M dotted with v .

With two 1D arrays np.dot returns a single number , the inner product: multiply matching elements and add them up (great for weighted totals like price × quantity). The identity matrix ( np.eye(n) ) is the matrix version of 1, and the transpose ( A.T ) flips rows and columns to make shapes line up.

Expected output: 32 twice, then the total cost 26.0 ( 2.5*4 + 3.0*2 + 1.0*10 ).

Expected output: A @ I equals A (as floats), the transpose [[1 3] [2 4]] , and A @ A.T = [[5 11] [11 25]] .

Replace each ___ so the program computes the matrix product of A and B.

Expected output: [[2 8] [7 19]] . (Answer: the @ operator.)

❌ ValueError: matmul: ... shapes ... not aligned

The inner dimensions don't match for matrix multiplication.

✅ Fix: an (m, n) matrix needs an (n, p) partner — transpose one operand with .T if needed.

* multiplied positions element-wise instead of doing a matrix product.

✅ Fix: use @ (or np.dot / np.matmul ) for matrix multiplication.

Each row of sales is one store's units sold for three products. Multiply by the price vector to get each store's total revenue with a single matrix-vector product.

Expected output: [ 60 85 150] and Best store index: 2 . (Store 1 is 0*10 + 4*20 + 1*5 = 85 ; store 2 is 5*10 + 5*20 + 0*5 = 150 .)

Lesson 16 complete — you've mastered the matrix product!

You now know that * is element-wise while @ , np.dot , and np.matmul are the true matrix product, and you used matrix-vector and 1D inner products.

🚀 Up next: Conditions — choose, mask, and bound values with np.where , np.select , and np.clip .

Practice quiz

What does the * operator do between two NumPy arrays?

  • Element-wise multiplication
  • Matrix multiplication
  • A dot product scalar
  • Concatenation

Answer: Element-wise multiplication. * multiplies matching positions element-wise, not the matrix product.

Which operator performs true matrix multiplication?

  • *
  • @
  • +
  • %

Answer: @. The @ operator does matrix multiplication; * is element-wise.

For A=[[1,2],[3,4]] and B=[[5,6],[7,8]], what is A @ B?

Matrix product: top-left is 1*5 + 2*7 = 19, giving [[19 22] [43 50]].

For the same A and B, what is the element-wise A * B?

Element-wise multiplies matching cells: [[5 12] [21 32]].

What does np.dot([1,2,3], [4,5,6]) return?

Two 1D arrays give the inner product 1*4 + 2*5 + 3*6 = 32.

For 2D arrays, which three give the same matrix product?

  • A * B, A + B, A - B
  • np.dot, np.matmul, and @
  • A.T, A, np.eye
  • sum, mean, max

Answer: np.dot, np.matmul, and @. np.dot(A,B), np.matmul(A,B), and A @ B all give the matrix product.

What does A.T do to a matrix A?

  • Inverts it
  • Doubles it
  • Transposes it (swaps rows and columns)
  • Sums it

Answer: Transposes it (swaps rows and columns). .T returns the transpose, flipping rows and columns.

An (m, n) matrix can be matrix-multiplied by a matrix of which shape?

  • (n, p)
  • (m, n)
  • (p, m)
  • (m, p)

Answer: (n, p). The inner dimensions must match: (m, n) @ (n, p) gives (m, p).

What does np.eye(2) create?

  • A 2x2 array of twos
  • A zero matrix
  • A row vector
  • The 2x2 identity matrix

Answer: The 2x2 identity matrix. np.eye(n) builds the n-by-n identity matrix.

Which error appears when inner dimensions do not match for @?

  • ValueError about shapes not aligned
  • ZeroDivisionError
  • KeyError
  • ImportError

Answer: ValueError about shapes not aligned. Mismatched inner dimensions raise a ValueError about shapes not aligned.