Open In App

Matrix manipulation in Python

Last Updated : 10 Dec, 2025
Comments
Improve
Suggest changes
28 Likes
Like
Report

In Python, matrices can be represented as 2D lists or 2D arrays. Using NumPy arrays for matrices provides additional functionalities for performing various operations efficiently. NumPy is a Python library that offers fast, optimized array operations.

Why Use NumPy for Matrix Operations?

  • Efficient Computation: Uses optimized C-level implementations.
  • Cleaner Code: Eliminates explicit loops in many operations.
  • Wide Functionality: Supports element-wise operations, matrix multiplication, aggregation, and more.

Matrix Operations in NumPy

1. Element-wise Addition, Subtraction, and Division

Performing element-wise operations allows you to directly apply arithmetic operations between matrices of the same shape.

Python
import numpy as np

x = np.array([[1, 2], [4, 5]])
y = np.array([[7, 8], [9, 10]])

print("Addition:\n", np.add(x, y))
print("Subtraction:\n", np.subtract(x, y))
print("Division:\n", np.divide(x, y))

Output

The element wise addition of matrix is:
[[ 8 10]
[13 15]]
The element wise subtraction of matrix is:
[[-6 -6]
[-5 -5]]
The element wise division of matrix is:
[[0 0]
[0 0]]

2. Element-wise Multiplication vs. Matrix Multiplication

Use np.multiply() for element-wise multiplication and np.dot() or @ for standard matrix multiplication.

Python
import numpy as np

x = np.array([[1, 2], [4, 5]])
y = np.array([[7, 8], [9, 10]])

print("Element-wise multiplication:\n", np.multiply(x, y))
print("Matrix multiplication:\n", np.dot(x, y))

Output

Element-wise multiplication of matrix is:
[[7 16]
[36 50]]
Matrix multiplication:
[[25 28]
[73 82]]

3. Other Useful NumPy Matrix Functions

NumPy provides utility functions to perform common matrix operations like square root, sum, or transpose.

Python
import numpy as np

x = np.array([[1, 2], [4, 5]])
y = np.array([[7, 8], [9, 10]])

print("Square root:\n", np.sqrt(x))
print("Sum of all elements:", np.sum(y))

print("Column-wise sum:", np.sum(y, axis=0))
print("Row-wise sum:", np.sum(y, axis=1))
print("Transpose:\n", x.T)

Output

The element wise square root is:
[[ 1. 1.41421356]
[ 2. 2.23606798]]
The summation of all matrix element is: 34
The column wise summation of all matrix is: [16 18]
The row wise summation of all matrix is: [15 19]
The transpose of given matrix is:
[[1 4]
[2 5]]

Matrix Operations Using Nested Loops

If you are not using NumPy, you can perform matrix operations using nested loops:

Python
A = [[1,2],[4,5]]
B = [[7,8],[9,10]]
rows = len(A)
cols = len(A[0])

C = [[0 for i in range(cols)] for j in range(rows)]
for i in range(rows):
    for j in range(cols):
        C[i][j] = A[i][j] + B[i][j]
print("Addition:\n", C)

D = [[0 for i in range(cols)] for j in range(rows)]
for i in range(rows):
    for j in range(cols):
        D[i][j] = A[i][j] - B[i][j]
print("Subtraction:\n", D)

E = [[0 for i in range(cols)] for j in range(rows)]
for i in range(rows):
    for j in range(cols):
        E[i][j] = A[i][j] / B[i][j]
print("Division:\n", E)

Output

Addition:
[[8, 10], [13, 15]]

Subtraction:|
[[-6, -6], [-5, -5]]

Division:
[[0.14285714285714285, 0.25], [0.4444444444444444, 0.5]]


Article Tags :

Explore