 # Python – Multiply two matrices

## Python program to multiply two matrices

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 column 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)`

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)`

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)`

Output:

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

Last Updated on October 28, 2021 by admin

## Ways to import CSV files in Google ColabWays to import CSV files in Google Colab

Ways to import CSV files in Google Colab Colab (short for Colaboratory) is Google’s free platform which enables users to code in Python. It is a Jupyter Notebook-based cloud service, provided by Google. This platform allows us to train the Machine

## Python – os.path.split() methodPython – os.path.split() method

Python | os.path.split() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used

## Python seek() functionPython seek() function

Python seek() function The concept of file handling is used to preserve the data or information generated after running the program. Like other programming languages like C, C++, Java, Python also support file handling. seek() method In Python, seek() function is

## Python List index() MethodPython List index() Method

Python List index()   Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the lowest index where the element appears. Syntax: list_name.index(element, start, end) Parameters:  element –

## Python List insert() MethodPython List insert() Method

Python List insert() The Python List insert() method is an inbuilt function in Python that inserts a given element at a given index in a list. Syntax: list_name.insert(index, element) Parameters: index: the index at which the element has to be inserted. element: the element

## Python – Check for URL in a StringPython – Check for URL in a String

Python | Check for URL in a String In this article, we will need to accept a string and we need to check if the string contains any URL in it. If the URL is present in the string, we