Detecting when matrix multiplication is possible
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Matrix multiplication is a fundamental operation in linear algebra, extensively used across various fields such as mathematics, computer science, physics, and engineering. It is crucial to understand the conditions under which the multiplication of two matrices is defined, as not all matrices can be multiplied together. In this article, we explore the conditions necessary for matrix multiplication, provide examples, and discuss related topics.
Conditions for Matrix Multiplication
Matrix multiplication is possible only if certain dimensional conditions are satisfied. Specifically, if you have two matrices A and B:
• Let A be of size (i.e., A has rows and columns). • Let B be of size (i.e., B has rows and columns).
For the product AB to be defined, the number of columns in matrix A must be equal to the number of rows in matrix B . When these dimensional conditions are met, the resulting product matrix AB will have dimensions .
Example
Consider two matrices:
Matrix A: • A is a matrix.
Matrix B: • B is a matrix.
Here, the number of columns in A (2) equals the number of rows in B (2), so AB is defined and the result will be a matrix.
Computing AB: AB = \begin{bmatrix} 1 & 2 \ 3 & 4 \ 5 & 6 \end{bmatrix} \begin{bmatrix} 7 & 8 \ 9 & 10 \end{bmatrix} \= \begin{bmatrix} (1 \times 7 + 2 \times 9) & (1 \times 8 + 2 \times 10) \ (3 \times 7 + 4 \times 9) & (3 \times 8 + 4 \times 10) \ (5 \times 7 + 6 \times 9) & (5 \times 8 + 6 \times 10) \end{bmatrix} \= \begin{bmatrix} 25 & 28 \ 57 & 64 \ 89 & 100 \end{bmatrix}
Key Points Summary
| Topic | Description |
| Matrix A dimensions | |
| Matrix B dimensions | |
| Multiplication condition | Number of columns in A equals the number of rows in B . |
| Resultant matrix AB dimensions | |
| Non-Commutativity of matrices | generally (order matters) |
| Associative property | |
| Distributive property |
Properties Relevant to Matrix Multiplication
Non-Commutative Nature
Matrix multiplication is not generally commutative, meaning that for two matrices A and B, the product AB may not equal BA. The conditions for BA to exist are different, and if BA exists, it can result in a matrix of different dimensions compared to AB.
Associative Property
Matrix multiplication is associative. For matrices A, B, and C, if the multiplication operations are defined, the equation holds true. Associativity is crucial in complex computations involving multiple matrices, as it allows flexibility in the order of operations.
Distributive Property
Matrix multiplication is distributive over addition. For matrices A, B, and C of appropriate dimensions, the following properties hold: • •
Conclusion
Understanding when matrix multiplication is possible is essential not only for theoretical studies but also for practical applications. It is pivotal in solving linear equations, transforming coordinates, and in many algorithms that underpin computing technologies. Recognizing the necessary conditions and properties of matrix multiplication can significantly enhance your ability to work with matrices effectively in various domains.
Related reading
- determine if a point sits inside an arbitrary shape?
- Determine if two rectangles overlap each other?
- Determine non-convex hull of collection of line segments
- Determine points within a given radius algorithm
- Determine the combinations of making change for a given amount
- Determining Floating Point Square Root
- Determining if a sphere intersects an object or not
- Determining if two rays intersect

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.