- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to Multiply Two Matrices using Python?
Multiplication of two matrices is possible only when number of columns in first matrix equals number of rows in second matrix.
Multiplication can be done using nested loops. Following program has two matrices x and y each with 3 rows and 3 columns. The resultant z matrix will also have 3X3 structure. Element of each row of first matrix is multiplied by corresponding element in column of second matrix.
X = [[1,2,3], [4,5,6], [7,8,9]] Y = [[10,11,12], [13,14,15], [16,17,18]] result = [[0,0,0], [0,0,0], [0,0,0]] # iterate through rows of X for i in range(len(X)): for j in range(len(Y)): for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] for r in result: print(r)
The result: [84, 90, 96] [201, 216, 231] [318, 342, 366]
- Related Articles
- Python program to multiply two matrices
- How can Tensorflow be used to multiply two matrices using Python?
- How to multiply two matrices using pointers in C?
- C# program to multiply two matrices
- Java program to multiply two matrices.
- Swift Program to Multiply two Matrices Using Multi-dimensional Arrays
- How to Add Two Matrices using Python?
- How to multiply two matrices by elements in R?
- Program to multiply two matrices in C++
- How to multiply corresponding values from two matrices in R?
- How to multiply two matrices in R if they contain missing values?
- How can Tensorflow be used to add two matrices using Python?
- C++ Program to Multiply two Matrices by Passing Matrix to Function
- Golang Program to Multiply two Matrices by Passing Matrix to a Function
- Swift Program to Multiply two Matrices by Passing Matrix to a Function