All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Multiplication Of Two Matrix In Python

Last Updated : Mar 11, 2024

Multiplication Of Two Matrix In Python

In this tutorial we will show you the solution of multiplication of two matrix in python, in python programming, use of matrix is very essential and common. We can perform various tasks and conditions with the use of matrix and its logic within the code.

In this tutorial, we’re going to learn and understand the way of doing multiplication of two matrices in python programming.

Step By Step Guide On Multiplication Of Two Matrix In Python :-

Method 1 – By Using Simple Nested Loop Method

x = [[5, 8, 4],
    [2, 1, 9],
    [3, 6, 7]]
y = [[7, 6, 4, 1],
    [6, 9, 5, 0],
    [4, 4, 6, 7]]
     result = [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]
for i in range(len(x)):
   for j in range(len(y[0])):
  for z in range(len(y)):
 result[i][j] += x[i][z] * y[z][j]
for r in result:
 print(r)
  1. Firstly, we declared two matrices in the code - X matrix of order 3x3 and Y matrix of order 3x4
  2. Now, we have declared a matrix z which has all the elements zero.
  3. Here, we used loop method as using ‘I’ and as number of rows of x matrix and then used range(len()) function. Within this function, we declared the values of matrix X.
  4. Similarly, we used the range(len()) function for the matrix Y and declared the columns by using the variable ‘j’.
  5. At the end, we used the same method for the ‘z’ matrix in order to get the desired result.
  6. Now, the final step, we declared result and within it, we used result[i][j] += x[i][z] * y[z][j] function.
  7. In result, we multiplied array of i and j where we multiplied them with the Z matrix to get the multiplied values.
  8. Now, we printed the result as using the print functions and eventually we will get the desired multiplied values of both the matrices.

Method 2 - Multiplication of two matrix by using Numpy -

In this method, we’re going to import the numpy library and then we’ll use its function to complete the task of multiplying two matrix.

This method can be termed as vectorized implementation also.

import numpy as np

x = [[1, 7, 3],
    [4, 4, 6],
    [7, 5, 1]]
y = [[1, 8, 1, 9],
    [5, 8, 5, 0],
    [2, 3, 2, 1]]
 result= [[0,0,0,0],
        [0,0,0,0],
        [0,0,0,0]]
 result = np.dot(x,y)
 for r in result:
    print(r)
  1. First, we imported the libraries of numpy and its functions within the code.
  2. We declared a matrix of order 3x3 as named – ‘x’.
  3. Again, we declared second matrix of a variable named ‘y’ of order 3x4.
  4. As the result will be of order 3x4, we again declared a matrix named ‘result’ of order 3x4 which has all the elements zero.
  5. Now, to do the multiplication of both the matrix, we used the np.dot() function and passed both the matix as arguments.
  6. The last step, we printed the result.

Conclusion :-

In this tutorial, we used two functions to multiply two matrix within the python code.

This condition can full fill various conditions within the code and can also help the developer to create the program accordingly.

I hope this tutorial on multiplication of two matrix in python helps you.