Matlab-Matrix – Multiplication


Matlab-Matrix – Multiplication



”;


Consider two matrices A and B. If A is an m x n matrix and B is an n x p matrix, they could be multiplied together to produce an m x n matrix C. Matrix multiplication is possible only if the number of columns n in A is equal to the number of rows n in B.

In matrix multiplication, the elements of the rows in the first matrix are multiplied with the corresponding columns in the second matrix.

Each element in the (i, j)thposition, in the resulting matrix C, is the summation of the products of elements in ith row of the first matrix with the corresponding element in the jth column of the second matrix.

Matrix multiplication in MATLAB is performed by using the * operator.

Example

Consider following example in MATLAB


a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
prod = a * b

Output

The execution in MATLAB will display the following result −


>> a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
prod = a * b


prod =

    18  10  -4
    27  14  -4
    22  16  -6
 
>>

The mtimes function

You can also make use of the function mtimes to multiply two given matrices. It is a builtin function available in MATLAB.

Example

Consider following example −


a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
test= mtimes(a,b)

Output

On execution in MATLAB the output is as follows −


>> a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
test= mtimes(a,b)


test =

    18  10  -4
    27  14  -4
    22  16  -6
      
>>

Advertisements

”;

Leave a Reply

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