Matrix Operations


>> In this section we will discuss the following basic and commonly used matrix operations:


1. Addition and Subtraction of Matrices


2. Division of Matrices


3. Scalar Operations of Matrices


4. Transpose of a Matrix


5. Concatenating Matrices


6. Matrix Multiplication


7. Determinant of a Matrix


8. Inverse of a Matrix



1. Addition and Subtraction of Matrices:


You can add or subtract matrices. Both the operand matrices must have the same number of rows and columns.



Example:



2. Division of Matrices


You can divide two matrices using left (\) or right (/) division operators. Both the operand matrices must have the same number of rows and columns.


Example:




3. Scalar Operations of Matrices:


When you add, subtract, multiply or divide a matrix by a number, this is called the scalar operation. Scalar operations produce a new matrix with same number of rows and columns with each element of the original matrix added to, subtracted from, multiplied by or divided by the number.



Example:



4. Transpose of a Matrix:


The transpose operation switches the rows and columns in a matrix. It is represented by a single quote( ' ).


Example:



6. Matrix Multiplication:


Consider two matrices A and B. If A is an m x n matrix and B is a 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 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.


In MATLAB, matrix multiplication is performed by using the * operator.



Example:



Matrix-Matrix Multiplication:



In the product of two matrices AB, the number of columns in A must equal the number of rows in B. The row-column multiplications form column vectors, and these column vectors form the matrix result. The product AB has the same number of rows as A and the same number of columns as B. For example,




Use the operator * to perform matrix multiplication in MATLAB. 

Do it yourself

A = [6,-2;10,3;4,7];

B = [9,8;-5,12];

A*B = ?

Matrix multiplication does not have the commutative property; that is, in general, AB is not equal to BA. A simple example will demonstrate this fact:




Reversing the order of matrix multiplication is a common and easily made mistake.


7. Determinant of a Matrix:


Determinant of a matrix is calculated using the det function of MATLAB. Determinant of a matrix A is given by det(A).



Example:



8. Inverse of a Matrix:


The inverse of a matrix A is denoted by A^−1 such that the following relationship holds:


(AA)^−1 = A^−1A = 1


The inverse of a matrix does not always exist. If the determinant of the matrix is zero, then the inverse does not exist and the matrix is singular.


In MATLAB, inverse of a matrix is calculated using the inv function. Inverse of a matrix A is given by inv(A).



Example: