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:
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 + Bgives the output
C =
3 6
6 9
and of course the operation C - A gives B.
A = [1 2; 3 4]; B = A * A C = B *2The 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)
>>2\3 ans = 1.5 >>2/3 ans = 0.6667Of 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/Athis 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)
Consider a system of two simultaneous linear equations containing two unknowns:
2x1 + 5x2 = 3 4x1 + 5x2 = 11This can be written in matrix form
A*X =Bwhere
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 programThe result of the program is
X =
4
-1
Hence we have solved the equation to get x1 = 4 and x2 = -1.
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 * Ahowever, 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.
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.