Matrices and Vectors


In [1]:
% The ; denotes we are going back to a new row.
A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]


A =

    1    2    3
    4    5    6
    7    8    9
   10   11   12


In [2]:
% Initialize a vector 
v = [1;2;3]


v =

   1
   2
   3


In [3]:
% Get the dimension of the matrix A where m = rows and n = columns
[m,n] = size(A)


m =  4
n =  3

In [4]:
% You could also store it this way
dim_A = size(A)


dim_A =

   4   3


In [5]:
% Get the dimension of the vector v 
dim_v = size(v)


dim_v =

   3   1


In [6]:
% Now let's index into the 2nd row 3rd column of matrix A
A_23 = A(2,3)


A_23 =  6

Addition and Scalar Multiplication


In [7]:
% Initialize matrix A and B 
A = [1, 2, 4; 5, 3, 2]
B = [1, 3, 4; 1, 1, 1]


A =

   1   2   4
   5   3   2

B =

   1   3   4
   1   1   1


In [8]:
% Initialize constant s 
s = 2


s =  2

In [9]:
% See how element-wise addition works
add_AB = A + B


add_AB =

   2   5   8
   6   4   3


In [10]:
% See how element-wise subtraction works
sub_AB = A - B


sub_AB =

   0  -1   0
   4   2   1


In [11]:
% See how scalar multiplication works
mult_As = A * s


mult_As =

    2    4    8
   10    6    4


In [12]:
% Divide A by s
div_As = A / s


div_As =

   0.50000   1.00000   2.00000
   2.50000   1.50000   1.00000


In [13]:
% What happens if we have a Matrix + scalar?
add_As = A + s


add_As =

   3   4   6
   7   5   4

Matrix Vector Multiplication


In [14]:
% Initialize matrix A 
A = [1, 2, 3; 4, 5, 6;7, 8, 9]


A =

   1   2   3
   4   5   6
   7   8   9


In [15]:
% Initialize vector v 
v = [1; 1; 1]


v =

   1
   1
   1


In [16]:
% Multiply A * v
Av = A * v


Av =

    6
   15
   24

Matrix Matrix Multiplication


In [17]:
% Initialize a 3 by 2 matrix 
A = [1, 2; 3, 4;5, 6]


A =

   1   2
   3   4
   5   6


In [18]:
% Initialize a 2 by 1 matrix 
B = [1; 2]


B =

   1
   2


In [19]:
% We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1) 
mult_AB = A*B


mult_AB =

    5
   11
   17

Matrix Multiplication Properties


In [20]:
% Initialize random matrices A and B 
A = [1,2;4,5]
B = [1,1;0,2]


A =

   1   2
   4   5

B =

   1   1
   0   2


In [21]:
% Initialize a 2 by 2 identity matrix
I = eye(2)
% The above notation is the same as I = [1,0;0,1]


I =

Diagonal Matrix

   1   0
   0   1


In [22]:
% What happens when we multiply I*A ? 
IA = I*A


IA =

   1   2
   4   5


In [23]:
% How about A*I ? 
AI = A*I


AI =

   1   2
   4   5


In [24]:
% Compute A*B 
AB = A*B


AB =

    1    5
    4   14


In [25]:
% Is it equal to B*A? 
BA = B*A


BA =

    5    7
    8   10


In [26]:
% Note that IA = AI but AB != BA

Inverse and Transpose


In [27]:
% Initialize matrix A 
A = [1,2,0;0,5,6;7,0,9]


A =

   1   2   0
   0   5   6
   7   0   9


In [28]:
% Transpose A 
A_trans = A'


A_trans =

   1   0   7
   2   5   0
   0   6   9


In [29]:
% Take the inverse of A 
A_inv = inv(A)


A_inv =

   0.348837  -0.139535   0.093023
   0.325581   0.069767  -0.046512
  -0.271318   0.108527   0.038760


In [30]:
% What is A^(-1)*A? 
A_invA = inv(A) * A


A_invA =

   1.0000e+00  -8.3267e-17   5.5511e-17
   2.7756e-17   1.0000e+00  -8.3267e-17
  -3.4694e-17   2.7756e-17   1.0000e+00

Effectively the identity matrix