Updated March 24, 2023
Introduction to Matrix Multiplication in Matlab
‘Matlab’ word represents Matrix laboratory. Initially, Matlab designed for the implementation of matrix operations. By using Matlab we can easily implement complex operations ad problems very easily. As we know in matrix operations multiplication is one of the difficult and complicated operations but by using simple command ‘mtimes’ we multiply two matrices.
There are some rules of matrix multiplication just like mathematics. If there are two matrices then a number of columns of the first matrix should be equal to the number of rows of the second column. Let us assume first matrix dimensions are 2 rows and 3 columns and second matrix dimensions are 4 rows and 3 columns then we cannot perform multiplication because a number of columns in the first matrix and number of rows in the second matrix are not the same.
How to Perform Matrix Multiplication in Matlab?
There are two ways to multiply matrix one is by using multiplication ‘*’ operator. And second is by using ‘mtimes command.
Using ‘ * ’ Operator
To multiply two matrices first we need two matrix. we can directly declare the matrices or we can accept input from the user. Here are some of the steps that we need to follow as given below:
- Step 1: accept two matrix by declaring two variables.
- Step 2: assign 3^{rd} variable for output and write a statement as matrix 1 * matrix 2.
- Step 3: display output.
Using ‘mtimes’ Command
In this method, there is no need for operators we can give the direct command to the input matrix. A statement can be written as mtimes ( matrix 1, matrix 2 )
- Step 1: accept two matrix by declaring two variables.
- Step 2: assign a 3^{rd} variable for output and give command mtimes.
- Step 3: display output.
Examples to Implement Matrix Multiplication
Here are some of the examples of matrix multiplication in Matlab which are given below:
Example #1
Let us consider two matrix mat1 and mat2,
mat 1 =
1 2 3
3 4 2
3 2 1
mat 2 =
1 1 1
3 4 2
3 2 1
Using ‘ * ’ Operator
The following table shows the above example by using the ‘ * ’ operator.
Matlab editor |
Output |
mat1 = [ 1 2 3 ; 3 4 2 ; 3 2 1 ]
mat2=[ 1 1 1; 3 4 2 ; 3 2 1 ] mat3 = mat1 * mat2 |
mat1 =
1 2 3 3 4 2 3 2 1 mat2 = 1 1 1 3 4 2 3 2 1 mat3 = 16 15 8 21 23 13 12 13 8 |
Output:
Using ‘mtimes’ Command
The following table shows the above example by using the ‘mtimes’ command.
Matlab Editor |
Output |
mat1= [ 1 2 3 ; 3 4 2 ; 3 2 1 ]
mat2=[ 1 1 1 ; 3 4 2 ; 3 2 1 ] mat4= mtimes ( mat1 , mat2 ) |
mat1 =
1 2 3 3 4 2 3 2 1 mat2 = 1 1 1 3 4 2 3 2 1 mat4 = 16 15 8 21 23 13 12 13 8 |
Output:
Example #2
Let us consider two matrix mat and mat 2 are,
mat1 =
23 32 11
22 3 2
16 39 21
32 4 1
mat2 =
41 11 43
32 41 32
3 2 1
In the above example, dimensions of the first matrix are 4 rows and 3 columns, and dimensions of the second matrix are 3 rows and 3 columns so the number of columns of the first matrix is equal to the number of rows of the second matrix so multiplication can be executed.
Using ‘ * ’ Operator
The following table shows the above example by using the ‘ * ’ operator.
Matlab editor |
Output |
mat1 = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]
mat2 = [ 4 1 11 43 ; 32 41 32 ; 3 2 1 ] mat3 = mat1 * mat 2 |
mat1 =
23 32 11 22 3 2 16 39 21 32 4 1 mat2 = 41 11 43 32 41 32 3 2 1 mat3 = 2000 1587 2024 1004 369 1044 1967 1817 1957 1443 518 1505 |
Output:
Using ‘mtimes’ Command
The following table shows the above example by using ‘mtimes’ command.
Matlab editor |
Output |
mat1 = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]
mat2 = [ 41 11 43 ; 32 41 32 ; 3 2 1 ] mat4 = mtimes ( mat1 , mat2 ) |
mat1 =
23 32 11 22 3 2 16 39 21 32 4 1 mat2 = 41 11 43 32 41 32 3 2 1 mat4 = 2000 1587 2024 1004 369 1044 1967 1817 1957 1443 518 1505 |
Output:
Example #3
Let us assume two matrices are mat1 and mat2,
mat1 =
5 6 3 2
3 2 4 5
3 2 1 1
mat2 =
3 4 2
2 3 4
3 3 4
In the above example, the dimension of the first matrix are 3 rows and 4 columns and dimensions of the second matrix are 3 rows and 3 columns so a number of columns of the first matrix are not equal to the number of rows of the second matrix so multiplication cannot execute.
Matlab editor |
Output |
mat1= [ 5 6 3 2 ; 3 2 4 5 ; 3 2 1 1 ]
mat2= [ 3 4 2 ; 2 3 4 ; 3 3 4 ] mat3= mat1 * mat2 |
mat1 =
5 6 3 2 3 2 4 5 3 2 1 1 mat2 = 3 4 2 2 3 4 3 3 4 Error using * Inner matrix dimensions must agree. |
Output:
Conclusion
Matrix multiplication is a very difficult and complex operation in mathematics but we implement the same in Matlab we can easily get the output without error. Both the methods used for matrix multiplication are easy and simple to implement. It makes the program more efficient.
Recommended Articles
This is a guide to Matrix Multiplication in Matlab. Here we discuss how to perform matrix multiplication in Matlab along with the examples. You may also look at the following articles to learn more –