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

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 Skew Symmetric Matrix
A skew-symmetric matrix is a square matrix whose transpose is equal to its negative. In other words, it is a matrix that satisfies the condition A^T = -A. This type...read more

All about Symmetric Matrix
All about Symmetric Matrix
A matrix is a rectangular arrangement of numbers (real or complex) or symbols arranged in rows and columns. The number in the matrix are called the elements, and if the...read more

Matrix Multiplication in C
Matrix Multiplication in C
A matrix is a collection of numbers organized in rows and columns. Matrices can be manipulated using operations like Addition, Subtraction, and Multiplication. Multiplying two matrices is only possible when...read more

Types of Matrix
Types of Matrix
In Linear Algebra, Matrices are one of the most important topics of mathematics. The application of matrix is not just limited to mathematical solving problems; it has its applications across...read more

Adjacency Matrix For Graphs
Adjacency Matrix For Graphs
An Adjacency Matrix is a method of representing graphs in matrix form. The adjacency matrix plays a vital role in describing finite graphs, making them easier to understand and compact...read more

Lower Triangular Matrix: Definition, Example, and Properties
Lower Triangular Matrix: Definition, Example, and Properties
Discover the essentials of lower triangular matrices in linear algebra. Explore their unique properties, practical applications in solving linear systems, and their significance in mathematical computations. Perfect for students and...read more

Transpose of a Matrix
Transpose of a Matrix
Transpose of a matrix is a matrix flipped over its main diagonal, switching the matrix’s rows and column indices. In this article, we will briefly discuss what transpose of a...read more

Confusion Matrix in Machine Learning
Confusion Matrix in Machine Learning
Are you tired of your AI models getting confused? Untangle their mysteries with the Confusion Matrix, your secret weapon for accuracy! Decode True Positives, False Negatives, and more to uncover...read more

Diagonal Matrix: Definition, Example, and Properties
Diagonal Matrix: Definition, Example, and Properties
A diagonal matrix is a special type of square matrix in which all non-diagonal entries are equal to zero, but all diagonal entries can either be zero or non-zero. This...read more

Identity Matrix: Definition, Examples, and Properties
Identity Matrix: Definition, Examples, and Properties
A square matrix of order n x n with ones on the main diagonal and zeros elsewhere is known as an Identity Matrix. From solving a system of linear equations...read more

Why, How, and When to Adopt a Matrix Organizational Structure
Why, How, and When to Adopt a Matrix Organizational Structure
Discover the meaning, types, advantages and disadvantages of the matrix organizational structure. This article delves into its real-world applications, guiding you through adoption steps, potential pitfalls, and when it's best...read more

Matrix Multiplication: A Beginner’s Guide to Understand and Implement
Matrix Multiplication: A Beginner’s Guide to Understand and Implement
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...read more

Upper Triangular Matrix: Definition, Example, and Properties
Upper Triangular Matrix: Definition, Example, and Properties
Explore the world of upper triangular matrices in our comprehensive guide. Understand their definition, properties, and practical applications in solving linear equations and beyond. Dive into the role of these...read more

How to Calculate the Determinant of a Matrix?
How to Calculate the Determinant of a Matrix?
The determinant of a matrix is a scalar value that is calculated from the elements of the Square matrix. It is used to determine whether a given matrix is invertible...read more
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