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 p 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 corresponding columns in the second matrix.

Each element in the (i, j)th position, in the resulting matrix C, is the summation of the products of elements in ith row of 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

Create a script file with the following code −

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

When you run the file, it displays the following result −

a =
      1     2     3
      2     3     4
      1     2     5
b =
      2     1     3
      5     0    -2
      2     3    -1
prod =
      18    10    -4
      27    14    -4
      22    16    -6
matlab_matrics.htm
Advertisements