Matrix arithmetic operations in MATLAB

When earlier we considered the multiplication and addition of simple (scalar) variables, we used normal mathematical operators. However, for calculations with matrix variables we will see that there are two types of arithmetic operations:

The results of the operations are generally quite different, and therefore, it is essential to determine which option you require before writing your program.

Matrix arithmetic

Addition and subtraction

Under the standard rules of matrix arithmetic, normal addition and subtraction are carried out on an element-by-element basis. For example

>> A = [1 2;3 4]  ;
>> B = [2 4;3 5];
>> C = A + B
gives the output
C =
      3   6
      6   9
and of course the operation C - A gives B.

Multiplication

Matrix multiplication is illustrated by the following program:
A = [1 2; 3 4];
B = A * A
C = B *2
The output of the program is
B =
     7    10
    15    22

C =
    14    20
    30    44
Notice that the multiplications are carried out under the standard rules of matrix arithmetic and thus the result of the first operation is not simply the square of all the elements but for this example is calculated as below:
B = (1*1+2*3)  (1*2+2*4)
    (1*3+4*3)  (3*2+4*4)

Division

In MATLAB both left and right division are possible. So if we wish to divide two scalars then two results are possible:
>>2\3
ans =
   1.5

>>2/3
ans =
   0.6667
Of course we can also use matrix division to reverse a multiplication e.g.
A = [1 2; 3 4];
C = [14 20; 30 40];
D = C/2
E = D/A
this program gives
D =
     7    10
    15    22
E =
     1     2
     3     4
In general if A is a square matrix then we can say that
A\B = inv(A)*B
A/B = B*inv(A)

Solving linear equations

Perhaps the most useful application of matrix division is in the solution of linear equations. In fact in MATLAB a system of n linear equations in n unknowns can be solved easily using left division (\). (The maths for over or under specified systems of equations is much more complicated and thus we won't consider it.).

Consider a system of two simultaneous linear equations containing two unknowns:

2x1 + 5x2 = 3
4x1 + 5x2 = 11
This can be written in matrix form
A*X =B
where
A = 2 5    X = x1    B =  3
    4 5        x2        11
Using left division we can write a program that solves this system of equations:
% Express system of equations in matrix form
A = [2,5;4,5];
B = [3;11];

% Now solve the system of equation
X = A\B
% End program
The result of the program is
X =
     4
    -1
Hence we have solved the equation to get x1 = 4 and x2 = -1.

Array arithmetic

Addition and subtraction

Matrix addition and subtraction are in fact carried out on an element-by-element basis and so there is no need for separate array addition and subtraction operations.

Multiplication

How can we square all the elements in matrix A?

If we write a program that calculates A^2 (this is equivalent to carrying out the matrix operation A*A) then we might write:

A = [1 2; 3 4];
B = A^2; % or B = A * A
however, this gives the output
B =
     7    10
    15    22
So we need to specify that we want to carry out the operation on an element-by-element basis, hence:
A = [1 2; 3 4];
C = A.^2; % or C = A .* A
C =
     1     4
     9    16
In general, the operation A(i,j).*B(i,j) will result in a matrix with elements aijbij.

Division

In a similar way to multiplication if we want to carry out division on an element-by-element basis then we used the array operations. Thus, the operation A(i,j)./B(i,j) will result in a matrix with elements aij/bij. Hence
A = [1 2; 3 4];
C = [1 4; 9 16];
D = C./A
D =
     1     2
     3     4
Note: For both array multiplication and division, unless either variable is a scalar, then both matrices being operated on must be the same size.

If you don't understand the rules of matrix and array arithmetic then it may help to refer to your maths notes or a standard maths text book.

Click HERE for next section.