Categories
3. Matrix

Matrix Multiplication

‘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 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 editorOutput
mat1 = [ 1 2 3 ; 3 4 2 ; 3 2 1 ]mat2=[ 1 1 1; 3 4 2 ; 3 2 1 ]mat3 = mat1 * mat2mat1 =1     2     33     4     23     2     1mat2 =1     1     13     4     23     2     1mat3 =16    15     821    23    1312    13     8

Output:

Matrix Multiplication in Matlab eg1

Using ‘mtimes’ Command

The following table shows the above example by using the ‘mtimes’ command.

Matlab EditorOutput
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     33     4     23     2     1mat2 =1     1     13     4     23     2     1mat4 =16    15     821    23    1312    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 editorOutput
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 2mat1 =23    32    1122     3     216    39    2132     4     1mat2 =41    11    4332    41    323     2     1mat3 =2000        1587        20241004         369        10441967        1817        19571443         518        1505

Output:

Matrix Multiplication in Matlab eg2.1

Using ‘mtimes’ Command

The following table shows the above example by using ‘mtimes’ command.

Matlab editorOutput
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    1122     3     216    39    2132     4     1mat2 =41    11    4332    41    323     2     1mat4 =2000        1587        20241004         369        10441967        1817        19571443         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 editorOutput
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 * mat2mat1 =5     6     3     23     2     4     53     2     1     1mat2 =3     4     22     3     43     3     4Error using *Inner matrix dimensions must agree.

Output:

eg3

Leave a Reply

Your email address will not be published. Required fields are marked *