Given two matrix the task is that we will have to create a program to multiply two matrices in python.

Examples:

Input : X = [[1, 7, 3], [3, 5, 6], [6, 8, 9]] Y = [[1, 1, 1, 2], [6, 7, 3, 0], [4, 5, 9, 1]] Output : [55, 65, 49, 5] [57, 68, 72, 12] [90, 107, 111, 21]

**Using Simple Nested Loops **

In this program we have to use nested for loops to iterate through each row and each column.

`# Program to multiply two matrices using nested loops ` ` ` `# take a 3x3 matrix ` `A ` `=` `[[` `12` `, ` `7` `, ` `3` `], ` ` ` `[` `4` `, ` `5` `, ` `6` `], ` ` ` `[` `7` `, ` `8` `, ` `9` `]] ` ` ` `# take a 3x4 matrix ` `B ` `=` `[[` `5` `, ` `8` `, ` `1` `, ` `2` `], ` ` ` `[` `6` `, ` `7` `, ` `3` `, ` `0` `], ` ` ` `[` `4` `, ` `5` `, ` `9` `, ` `1` `]] ` ` ` `result ` `=` `[[` `0` `, ` `0` `, ` `0` `, ` `0` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `0` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `0` `]] ` ` ` `# iterating by row of A ` `for` `i ` `in` `range` `(` `len` `(A)): ` ` ` ` ` `# iterating by coloum by B ` ` ` `for` `j ` `in` `range` `(` `len` `(B[` `0` `])): ` ` ` ` ` `# iterating by rows of B ` ` ` `for` `k ` `in` `range` `(` `len` `(B)): ` ` ` `result[i][j] ` `+` `=` `A[i][k] ` `*` `B[k][j] ` ` ` `for` `r ` `in` `result: ` ` ` `print` `(r) ` |

*chevron_right*

*filter_none*

Output:

[114, 160, 60, 27] [74, 97, 73, 14] [119, 157, 112, 23]

**Method 2: ** Matrix Multiplication Using Nested List. We use zip in Python.

`# Program to multiply two matrices using list comprehension ` ` ` `# take a 3x3 matrix ` `A ` `=` `[[` `12` `, ` `7` `, ` `3` `], ` ` ` `[` `4` `, ` `5` `, ` `6` `], ` ` ` `[` `7` `, ` `8` `, ` `9` `]] ` ` ` `# take a 3x4 matrix ` `B ` `=` `[[` `5` `, ` `8` `, ` `1` `, ` `2` `], ` ` ` `[` `6` `, ` `7` `, ` `3` `, ` `0` `], ` ` ` `[` `4` `, ` `5` `, ` `9` `, ` `1` `]] ` ` ` `# result will be 3x4 ` `result ` `=` `[[` `sum` `(a ` `*` `b ` `for` `a, b ` `in` `zip` `(A_row, B_col)) ` ` ` `for` `B_col ` `in` `zip` `(` `*` `B)] ` ` ` `for` `A_row ` `in` `A] ` ` ` `for` `r ` `in` `result: ` ` ` `print` `(r) ` |

*chevron_right*

*filter_none*

Output:

[114, 160, 60, 27] [74, 97, 73, 14] [119, 157, 112, 23]

**Method 3: ** Matrix Multiplication (Vectorized implementation).

`# Program to multiply two matrices (vectorized implementation) ` ` ` `# Program to multiply two matrices (vectorized implementation) ` `import` `numpy as np ` `# take a 3x3 matrix ` `A ` `=` `[[` `12` `, ` `7` `, ` `3` `], ` ` ` `[` `4` `, ` `5` `, ` `6` `], ` ` ` `[` `7` `, ` `8` `, ` `9` `]] ` ` ` `# take a 3x4 matrix ` `B ` `=` `[[` `5` `, ` `8` `, ` `1` `, ` `2` `], ` ` ` `[` `6` `, ` `7` `, ` `3` `, ` `0` `], ` ` ` `[` `4` `, ` `5` `, ` `9` `, ` `1` `]] ` ` ` `# result will be 3x4 ` ` ` `result` `=` `[[` `0` `,` `0` `,` `0` `,` `0` `], ` ` ` `[` `0` `,` `0` `,` `0` `,` `0` `], ` ` ` `[` `0` `,` `0` `,` `0` `,` `0` `]] ` ` ` `result ` `=` `np.dot(A,B) ` ` ` `for` `r ` `in` `result: ` ` ` `print` `(r) ` |

*chevron_right*

*filter_none*

Output:

[114, 160, 60, 27] [74, 97, 73, 14] [119, 157, 112, 23]

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Java Program to Multiply two Matrices of any size
- Program to multiply two matrices
- Multiply matrices of complex numbers using NumPy in Python
- Python program to add two Matrices
- Python program to add two matrices
- Python List Equality | Program to check if two given matrices are identical
- Program for addition of two matrices
- Program to check if two given matrices are identical
- Java Program to Add two Matrices
- Program to concatenate two given Matrices of same size
- Program to multiply two Matrix by taking data from user
- Multiplication of two Matrices in Single line using Numpy in Python
- Python - Multiply two list
- Kronecker Product of two matrices
- Count pairs from two sorted matrices with given sum
- Find the intersection of two Matrices
- Minimum elements to be added so that two matrices can be multiplied
- Check if the given two matrices are mirror images of one another
- Program for subtraction of matrices
- Adding and Subtracting Matrices in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.