Matlab-Matrix - Subtraction



To subtract two matrices, both the operand matrices must have the same number of rows and columns.

Example

Here is an example −

a = [ 1 2 3 ; 4 5 6; 7 8 9];
b = [ 7 5 6 ; 2 0 8; 5 7 1];
c = a - b

Output

On execution in MATLAB the result is as follows −

>> a = [ 1 2 3 ; 4 5 6; 7 8 9];
b = [ 7 5 6 ; 2 0 8; 5 7 1];
c = a - b

c =

   -6  -3  -3
    2   5  -2
    2   1   8
   
>>

The minus() Function

You can also make use of the minus() built-in function to subtract two matrices.

Example

Consider the following example for use of minus() function for subtraction of two matrices −

a = [ 1 2 3 ; 4 5 6; 7 8 9];
b = [ 7 5 6 ; 2 0 8; 5 7 1];
c = minus(a , b)

Output

You will get the following result −

>> a = [ 1 2 3 ; 4 5 6; 7 8 9];
b = [ 7 5 6 ; 2 0 8; 5 7 1];
c = minus(a , b)

c =

   -6  -3  -3
    2   5  -2
    2   1   8
 
>>
Advertisements