echo on
%%%%% ch00_mat.m %%%%%
% Run script and study the effects of each line.

echo off
disp(sprintf('**** Exercises for practising MatLab ****'));
disp(sprintf('**** MATRICES ****'));

echo on
format compact

A=[1,2,3,5;1,1,1,1;3,0,3,3]	% Create a m x n = 3 x 4 matrix. 
				% element a_ij; i=1..m lines, and  j=1..n columns
B=A'			% Transposed 4x3 matrix

D=5.1*A			% A scalar can multiply a matrix of any size.

D=D+A			% Matrices of same dimensions can be added or subtracted
			% d_ij = d_ij + a_ij for all i and j

D=D-A			% d_ij = d_ij - a_ij for all i and j

E=D(2:3,2)	% vector of entries from 2 to 3 of column 2

E=D(3,2:4)	% row vector from 3th row of entries from 2 to 4

E=D(2:3,1:2)	% square submatrix from rows 2 and 3 and columns 1 and 2

R=rand(4,4)		% Create a 4 x 4 matrix with random elements. 

I=eye(4)		% Create the identity 4 x 4 matrix. 

C1 = R*I		% Identity matrix does not change the result.

C=A*B			% Matrix multiplication C=A*B is the linear algebraic product 
			% of the matrices A and B.  c_ij=sum_(k=1..n)(a_ik*b_kj). 
			% In this case C is a symmetric matrix.

C1=B*A			% The matrix multiplication is not commutative A*B =! B*A
			% However, it is associative A*(B*C) = (A*B)*C and
			%                distributive A*(B+C) = A*B + B*C

E=[0,1;0,0]		% Create a 2 x 2 matrix. 

F=[1,0;0,0]		% And another 2 x 2 matrix. 

Z=E*F			% Result is zero even if E and F are not zero!

C2=inv(C)		% Inverse of the square matrix C.

C2=C/C			% Slash or matrix right division A/B is roughly the same as A*inv(B).

C2=C./C			% Array right division A./B is the matrix with elements A(i,j)/B(i,j).
			% A and B must have the same size, unless one of them is a scalar. 

rank(C)			% Provides an estimate of the number of linearly independent rows 
			% or columns of a matrix.

size(C1)		% return the size of a matrix

echo off		% print the size of C1 in both dimensions

disp(sprintf('C1 matrix is %g by %g', size(C1,1), size(C1,2))) 
