% MATLAB EXPLANATION
%
% Example 2 - MATRIX MANIPULATIONS
%
% MATLAB is really good at performing matrix calculations. For example,
% if you have a 3 x 3 matrix, all the elements of that matrix can be
% multiplied by a scalar (constant) in a single line:
A = [1,1,-1;
1,2,3;
2,-1,-2];
B = 3;
C1 = A*B
C2 = A+B
% This short program will multiply each element of matrix A by 3 to
% find C1, and add 3 to each element of A to find C2. The results
% are displayed on the screen as:
% C1 =
%
% 3 3 -3
% 3 6 9
% 6 -3 -6
% C2 =
%
% 4 4 2
% 4 5 6
% 5 2 1
% But other types of matrix operations such as multiplication can be
% executed in the same way.
%
D1 = [2;-1;3];
D2 = [2,-1,3];
E1 = A*D1
E2 = D2*A
% Will perform matrix multiplication. Note that D1 is a matrix having one
% column and three rows, while D2 is a matrix having three rows and one
% column. In matrix multiplication, the order of the operation is
% important, and it requires that the number of columns in the first
% matrix be equal to the number of rows in the second matrix. So, in this
% case:
% E1 = [2*1 + (-1)*1 + 3*(-1);
% 2*1 + (-1)*2 + 3*3;
% 2*2 + (-1)*(-1) + 3*(-2)]
% or
% E1 = [-2;9;-1]
% Obviously, the algorithms can become more compact (and complex):
F1 = 5 - (D2*A)*(A*D1)