VOOZH about

URL: https://www.geeksforgeeks.org/python/matrix-manipulation-python/

⇱ Matrix manipulation in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Matrix manipulation in Python

Last Updated : 10 Dec, 2025

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.

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.

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.

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:

Output

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

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

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

Related Articles:

Comment
Article Tags: