Matlab-Matrix - Addition



To add 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 =

    8  7   9
    6  5  14
   12 15  10
 
>>

The plus() function

You can also make use of plus() built-in function to add two matrices as shown below −

Example

Consider the following example for the use of plus() function to add the two matrices −

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

Output

The execution in MATLAB is as shown below −

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

c =

    8   7   9
    6   5  14
   12  15  10
 
>>
Advertisements