Matrix Multiplication: A Beginner’s Guide to Understand and Implement

Matrix Multiplication: A Beginner’s Guide to Understand and Implement

2 mins read954 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Apr 12, 2024 11:01 IST

Matrix multiplication is a binary operation whose output is also a binary operation. If A and B are two matrices of order m x n and n x p, then the order of the output matrix will be m x p. In this article, we will discuss the rule to multiply two matrices, properties of matrix multiplication and how to implement it in Python.

2022_12_MicrosoftTeams-image-98-2.jpg

A matrix is a rectangular arrangement of rows and columns, and if a matrix has m rows and n columns, then it is said to be of a matrix of order m x n. It is one of the most important topics of Linear Algebra, which is most commonly used in data science to store data and solve linear equations systems. In this article, we will discuss one of the algebraic operations of the Matrix, i.e., Matrix Multiplication in Python.

Must Check: Top Math Courses for Data Science

Table of Content

Recommended online courses

Best-suited Maths for Data Science courses for you

Learn Maths for Data Science with these high-rated online courses

Free
12 weeks
– / –
12 weeks
– / –
12 weeks
1 K
4 weeks
– / –
12 weeks
– / –
12 weeks
– / –
12 weeks
Free
8 weeks
– / –
12 weeks

Matrix Multiplication

Matrix multiplication is a binary operation whose output is also a binary operation. If A and B are two matrices of order m x n and n x p, then the product will be represented by:

AB = X,

and the order of X will be m x p.

Note: AB is defined if and only if the number of columns of a matrix A is equal to the number of rows of B.

Rule to Multiply two Matrices.

Let A and B be two matrices of order m x n and n x p, such that:

Now, let’s take some examples to get a better understanding of matrix multiplication.

Examples

1: Product of Square Matrices 

2022_12_image-44.jpg

Must Check: Square Matrix

2: Product of Non-Square Matrices 

2022_12_image-45.jpg
2022_12_image-46.jpg

3Product of a column and row matrix

2022_12_image-47.jpg

Properties of Matrix Multiplication

  • Matrix multiplication is non-commutative.
    • i.e., if A and B are two matrices, then AB != BA.
  • Matrix multiplication is associative in nature.
    • i.e., (AB)C = A(BC)
  • Matrix multiplication is distributive in nature.
    • i.e., (A + B) C = AC + BC and A (B + C) = AB + AC
  • Identity Property of Multiplication
    • i.e., IA = AI = A, where I is an Identity matrix.
  • Multiplicative Property of Zero
    • If a matrix is multiplied by a zero matrix, the result will be a zero matrix.
    • The product of two non-zero matrices may be a zero matrix.
    • If AB = 0, it doesn’t imply that A = 0 or B = 0.
  • Determinant 
    • det (AB) = det (A) det (B)
  • Transpose
    • (AB)T = BTAT
Types of Matrices Transpose of a Matrix
Symmetric Matrix Skew-Symmetric Matrix 

 

Matrix Multiplication in Python


 
# encoding the two matrices
A = [[1,2,3],[4, 5, 6],[7,8, 9]]
B = [[3,4],[5, 6],[7, 8]]
# retrieving the sizes/dimensions of the matrices
m1 = len(A)
n1 = len(A[0])
m2 = len(B)
n2 = len(B[0])
if(n1!=m2):
print("Error! Matrix sizes are not compatible")
quit()
# creating the product matrix of dimensions p×r
# and filling the matrix with 0 entries
C = []
for row in range(m1):
curr_row = []
for col in range(n2):
curr_row.append(0)
C.append(curr_row)
# performing the matrix multiplication
for i in range(m1):
for j in range(n2):
curr_val = 0
for k in range(n1):
curr_val += A[i][k]*B[k][j]
C[i][j] = curr_val
print(C)
Copy code

Output

2022_12_image-59.jpg

 

What is Programming What is Python
What is Data Science What is Machine Learning
Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Conclusion

In this article, we have discussed how to multiply two matrices, the properties of matrix multiplication, and finally, how to implement matrix multiplication in Python.

Hope this article will help you to know more about matrix multiplication.

All About Skew Symmetric Matrix

All about Symmetric Matrix

Matrix Multiplication in C

Types of Matrix

Adjacency Matrix For Graphs

Lower Triangular Matrix: Definition, Example, and Properties

Transpose of a Matrix

Confusion Matrix in Machine Learning

Diagonal Matrix: Definition, Example, and Properties

Identity Matrix: Definition, Examples, and Properties

Why, How, and When to Adopt a Matrix Organizational Structure

Matrix Multiplication: A Beginner’s Guide to Understand and Implement

Upper Triangular Matrix: Definition, Example, and Properties

How to Calculate the Determinant of a Matrix?
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio