Here matrix division means element-wise division: C[i][j] = A[i][j] / B[i][j]. Both matrices need the same shape, and no cell in B may be zero.
Remember
A ÷ B (entrywise) is defined when shapes match and B has no zeros
[ 8 6 ] [ 2 3 ] [ 4.0 2.0 ]
[ 9 4 ] ÷ [ 3 2 ] = [ 3.0 2.0 ]
Rule: (A ÷ B)[i][j] = A[i][j] / B[i][j]
Note: this is not A × B⁻¹ from linear algebra.
Position
A
B
A / B
[0][0]
8
2
4.0
[0][1]
6
3
2.0
[1][0]
9
3
3.0
[1][1]
4
2
2.0
Approach
How to Divide Matrices in Python
Four clear steps — same nested loops as addition, plus a zero guard.
Confirm both matrices have the same rows and columns.
Reject any zero in B before dividing.
Create a result list of lists of that size.
Set result[i][j] = A[i][j] / B[i][j] for every cell.
Method
Idea
Time
Best for
Nested loops
Divide matching cells into a new matrix
O(m × n)
Interviews, teaching
Safe divide
Validate shape + no zeros in B
O(m × n)
Safer production-style code
Pseudocode
Pseudocode
function DivideMatrices(A, B):
rows = rows of A
cols = cols of A
if rows != rows of B or cols != cols of B:
error "shapes must match"
C = new matrix[rows][cols]
for i from 0 to rows - 1:
for j from 0 to cols - 1:
if B[i][j] == 0:
error "division by zero"
C[i][j] = A[i][j] / B[i][j]
return C
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Rule
C[i][j] = A[i][j] / B[i][j]
Requirement
Same shape; B[i][j] ≠ 0
Storage
List of lists, e.g. [[8, 6], [9, 4]]
Division
Python 3 / returns a float
Not inverse
This is not A × B⁻¹
Classic check
[[8,6],[9,4]] ÷ [[2,3],[3,2]] = [[4,2],[3,2]]
Try it
Live Preview
Pick a sample pair to see matrices A, B, and A ÷ B (entrywise).
Tap a chip to run. All samples use nonzero divisors.
Result
A =
8 6
9 4
B =
2 3
3 2
A ÷ B =
4.00 2.00
3.00 2.00
Trace
Worked Walkthrough — 2×2
Visit every index once and divide the matching cells.
Start with a clear nested-loop divider. Then add shape and zero checks for safer code.
Example 1 — Nested Loops
Divide two same-sized matrices cell by cell into a new result matrix.
python
def divide_matrices(a, b):
rows = len(a)
cols = len(a[0])
result = [[0.0] * cols for _ in range(rows)]
for i in range(rows):
for j in range(cols):
result[i][j] = a[i][j] / b[i][j]
return result
def print_matrix(m):
for row in m:
print(" ".join(f"{val:.2f}" for val in row))
a = [[8, 6], [9, 4]]
b = [[2, 3], [3, 2]]
print_matrix(divide_matrices(a, b))
Output
4.00 2.00
3.00 2.00
How It Works
Read rows with len(a) and columns with len(a[0]).
Build a float result matrix with a list comprehension.
Outer loop chooses row i; inner loop chooses column j.
Python 3 / keeps decimals — 6 / 3 becomes 2.0.
Print with :.2f for two decimal places.
Example 2 — Shape & Zero Safe
Validate matching dimensions and reject any zero divisor before dividing.
python
def divide_matrices(a, b):
rows = len(a)
cols = len(a[0])
if len(b) != rows or any(len(row) != cols for row in a + b):
raise ValueError("Matrices must have the same dimensions.")
result = [[0.0] * cols for _ in range(rows)]
for i in range(rows):
for j in range(cols):
if b[i][j] == 0:
raise ZeroDivisionError(f"B[{i}][{j}] is zero.")
result[i][j] = a[i][j] / b[i][j]
return result
a = [[10, 9, 8], [6, 5, 4]]
b = [[2, 3, 4], [2, 1, 2]]
q = divide_matrices(a, b)
for row in q:
print(" ".join(f"{val:.2f}" for val in row))
Output
5.00 3.00 2.00
3.00 5.00 2.00
How It Works
Compare row counts and every row length; raise if shapes differ.
Before each division, check b[i][j] == 0.
Raise ZeroDivisionError with the failing index.
Otherwise store a[i][j] / b[i][j].
Mention both checks in interviews — shape and zero are the two traps.
Edge Cases & Pitfalls
Check these before calling the solution done.
Zero in B
Never divide
Guard every B[i][j] before using /.
// vs /
Use true division
// truncates. Use / when you want decimals like 2.5.
Shape
Must match
Different sizes cannot be divided entrywise.
Inverse
Different topic
True matrix division via B⁻¹ is not this algorithm.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops
O(m × n)
O(1) besides result
Shape & zero safe
O(m × n)
O(1) besides result
Every cell is touched exactly once. The result matrix needs O(m × n) space.