EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matrix Multiplication in Matlab
 

Matrix Multiplication in Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

Matrix-Multiplication-in-MATLAB

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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 3rd 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 3rd 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:

Matrix Multiplication in Matlab eg1

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:

Matrix Multiplication in Matlab eg2

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:

Matrix Multiplication in Matlab eg2.1

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:

eg2.2

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:

eg3

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 –

  1. Matrix Multiplication in Java
  2. 3D Matrix in MATLAB
  3. Matrix in Matlab
  4. Matlab AND Operator

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW