Hello! Welcome to the second lesson in our "Mathematical Foundations: Linear Algebra" module.
In the previous lesson, we explored vectors, understanding them as 1D arrays with both magnitude and direction. We learned to perform fundamental vector operations like the dot product, cross product, and projections, both mathematically and by using the NumPy library. This gave you the first taste of how linear algebra concepts are implemented in code.
Today, we'll build on that foundation by moving from 1D vectors to 2D matrices. This lesson directly addresses the learning outcome: Perform matrix operations (addition, multiplication) both manually and using NumPy. We will first cover the rules for performing these operations by hand, then see how to execute them efficiently in Python. Mastering matrix operations is crucial, as matrices are the primary tool for representing datasets, systems of linear equations, and transformations in machine learning and data science.
1. What is a Matrix?
Just as a vector is an ordered list of numbers (a 1D array), a matrix is a rectangular array of numbers arranged into rows and columns (a 2D array). A matrix with rows and columns is said to have dimensions or size .
For a quick and clear introduction to what a matrix is, how to describe its size, and how to reference its individual elements, please watch the first few minutes of the following video.
Matrices Top 10 Must Knows (ultimate study guide)
This video from JensenMath provides a concise definition of a matrix, its dimensions (order), and how to identify specific elements.
Watch from the beginning until 03:02. Focus on understanding the row-by-column convention for matrix dimensions (m x n) and the notation for matrix elements (e.g., A_ij for the element in row i, column j).
2. Manual Matrix Operations
Just like with numbers, we can perform arithmetic operations on matrices. However, there are specific rules that must be followed.
a) Addition, Subtraction, and Scalar Multiplication
These are the most straightforward operations.
- Addition and Subtraction: To add or subtract two matrices, they must have the exact same dimensions. The operation is performed element-wise, meaning you add or subtract the corresponding elements in each position.
- Scalar Multiplication: To multiply a matrix by a scalar (a single number), you simply multiply every single element in the matrix by that scalar. The dimensions of the matrix do not change.
Let's watch a video that illustrates these basic operations.
Matrices Top 10 Must Knows (ultimate study guide)
Let's continue with the JensenMath video, which demonstrates matrix addition, subtraction, and scalar multiplication with clear examples.
Watch from 03:02 to 06:09. Observe how the element-wise operations work for addition and how scalar multiplication distributes to every element.
b) Matrix Multiplication
Matrix multiplication is more complex than addition. It is not an element-wise operation. There are two critical rules to remember:
-
Dimension Compatibility: To multiply matrix by matrix (i.e., ), the number of columns in must equal the number of rows in .
- If is an matrix and is an matrix, you can multiply them.
- The resulting matrix, , will have dimensions .
-
The Calculation Process: Each element in the resulting matrix is calculated by taking the dot product of a row from matrix and a column from matrix .
- Specifically, the element (in row , column ) is the dot product of the -th row of and the -th column of .
The following image provides a fantastic visual breakdown of this process for two matrices.

Now, let's watch a detailed video explanation of this process.
Matrices Top 10 Must Knows (ultimate study guide)
The final segment from the JensenMath video details the process of matrix multiplication, emphasizing the dimension rule and the row-by-column dot product method.
Watch from 19:46 to 22:18. Pay close attention to how the dimensions of the matrices determine the dimension of the result and how each element of the product matrix is calculated.
A crucial property of matrix multiplication is that it is not commutative, which means in general, . In fact, if is a valid operation, might not even be possible if the dimensions don't align correctly.
For a more formal mathematical definition of matrix multiplication, which you might appreciate from your engineering studies, you can refer to the resource below. It presents the operation using summation notation.
Matrix Algebra for Engineers - HKUST Math Department
The textbook 'Matrix Algebra for Engineers' provides a formal definition of matrix operations.
Read the section titled 'Lecture 2 | Addition and multiplication of matrices' (starts on page 3). Focus on the general rule for matrix multiplication and the formal summation formula for the element c_ij. You don't need to do the problems yet.
Test your understanding!
Let and .
- Calculate .
- Calculate the matrix product .
Show answer
-
First, calculate :
Now, subtract this from : -
To calculate , we perform row-by-column dot products:
3. Matrix Operations with NumPy
Now that you understand the theory, let's put it into practice. As you saw with vectors, NumPy is the tool of choice for numerical operations in Python.
First, let's represent a matrix in NumPy. We do this by creating a 2D array, which is simply a list of lists.
import numpy as np
# A 2x3 matrix
A = np.array([
[1, 2, 3],
[4, 5, 6]
])
# A 3x2 matrix
B = np.array([
[7, 8],
[9, 10],
[11, 12]
])
Addition, Subtraction, and Element-wise Multiplication
NumPy's operators + and - work exactly as you'd expect, performing element-wise addition and subtraction.
The * operator, however, performs element-wise multiplication, not matrix multiplication. This is an important distinction! Element-wise multiplication requires the matrices to have the same dimensions.
True Matrix Multiplication
For true matrix multiplication (the row-by-column dot product we learned), NumPy provides two main ways:
- The
np.matmul()function. - The
@operator (introduced in Python 3.5), which is a convenient shorthand.
Let's watch a video that demonstrates all of these operations in NumPy.
Python Tutorial: Matrix Operations using Numpy for Absolute Beginners
This tutorial from Mr. STEM EDU TV clearly shows how to perform matrix addition, subtraction, and multiplication using NumPy.
Watch from 00:29 to 05:32. Pay special attention to: How + and - are used for addition and subtraction (00:29 - 02:33). How to use np.matmul() for true matrix multiplication (03:03 - 05:32). The error that occurs when matrix dimensions are not compatible for an operation.
Code Example
Let's put this all together in a single code block.
import numpy as np
# Define two 2x2 matrices
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[0, -1]])
print("Matrix A:\n", A)
print("Matrix B:\n", B)
# Addition
C_add = A + B
print("\nA + B:\n", C_add)
# Scalar Multiplication
C_scalar_mult = 2 * A
print("\n2 * A:\n", C_scalar_mult)
# Element-wise multiplication (NOT matrix multiplication)
C_elementwise = A * B
print("\nElement-wise A * B:\n", C_elementwise)
# True Matrix Multiplication
C_matmul = np.matmul(A, B)
# Or using the @ operator
C_matmul_operator = A @ B
print("\nMatrix Multiplication A @ B (using np.matmul):\n", C_matmul)
print("\nMatrix Multiplication A @ B (using @):\n", C_matmul_operator)
# Note that A @ B is not the same as B @ A
C_ba = B @ A
print("\nMatrix Multiplication B @ A:\n", C_ba)
Compare the output for A @ B with the result you calculated manually. They should match!
Conclusion
In this lesson, we demystified the fundamental operations of matrix algebra. You now have the skills to manipulate matrices both by hand and programmatically, a core competency for anyone working in data science.
Key Takeaways:
- Matrix Addition/Subtraction: Requires matrices of the same dimensions and is performed element-wise. In NumPy, this is done with the
+and-operators. - Scalar Multiplication: A scalar is multiplied with every element of the matrix.
- Matrix Multiplication: The number of columns in the first matrix must equal the number of rows in the second. The result is calculated via row-by-column dot products.
- NumPy Implementation: True matrix multiplication is done using
np.matmul(A, B)or theA @ Boperator. TheA * Boperator performs element-wise multiplication. - Non-Commutativity: Remember, .
In our next lesson, we will apply these new skills to a very practical problem: solving systems of linear equations using Gaussian elimination. You'll see how a system of equations like 2x + 3y = 8 can be represented and solved efficiently using matrices.