Matrices

Matrix Operators

Consider the matrix:

A = [1, 0, 5; 2, 1, 6; 3, 4, 0];

Written as:

A =
    1    0    5
    2    1    6
    3    4    0


There are many useful tool to manipulate matrices on Matlab. Some include:


The inverse matrix:

B = inv(A)

E =
   -24    20   -5
    18   -15    4
     5    -4    1


The transpose matrix:

C = A'

C =
    1    2    3
    0    1    4
    5    6    0


The determinant:

det(A)

ans =
    1


The eigenvalues and eigenvectors:

[V, D] = eig(A)

V =
    -0.4529   -0.7899    -0.5484
    -0.6883    0.5914    -0.4820
    -0.5667    0.1621     0.6833
D =
    7.2560          0          0
         0    -0.0264          0
         0          0    -5.2297


Where V is the matrix of columnar eigenvectors and D is a diagonal matrix of eigenvalues corresponding to each eigenvector.



Matrix Calculations

Consider the following matrices

A = [1, 2; 3, 4];
B = [5, 6; 7, 8];

A =
    1    2
    3    4

B =
    5   6
    7   8


Addition:

A + B

ans =
     6    8
    10   23


Subtraction:

B - A

ans =
    4    4
    4    4


Multiplication:

A * B

ans =
    19    22
    43    50


Division:

B / A

ans =
    -1    2
    -2    3


N.B. As with normal matrix multiplication, A*B ≠ B*A.



Element-Wise Multiplication:

A .* B

ans =
     5    12
    21    32


Element-Wise Division:

B ./ A

ans =
      5    3
    7/3    2


The dot (.) means 'element-wise'. As such, the elements in the first matrix are multiplied or divided by the elements in the other matrix with the same indices and, therefore, the matrices need to be of similar size.



Exponents:

A ^ 2

ans =
     7    10
    15    22


Element-Wise Exponents:

A .^ 2

ans =
     1    4
     9   16


In the first case, the matrix A is timed by itself. In the second case, each element is timed by itself individually.



Dot Product:

dot(A,B)

ans =
     26   44


Cross Product:

cross(A,B)

(Only performable on matrices with
at least one dimension of length 3.)


These are vectors operation and these matrices, as mentionned in 'Getting Started', are treated as collections of vectors.