Matrix Operations

From ControlTheoryPro.com

Jump to: navigation, search
Symbol.gif
Matrix Operations
Green carrot left.gif
All MATLAB Articles Special Matrices
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

1 Matrix Operations

1.1 Multiplication

In Linear Algebra 2 matrices can be multiplied by each only if the inner dimensions are equal.

LaTeX: A=BC


where
LaTeX: B is a matrix of n rows and m columns
LaTeX: C is a matrix of m rows and o columns
then LaTeX: A must be a matrix of n rows and o columns.

Notice that C has the same number of rows (m) and B does columns - the inner dimensions of the product. This also implies that the order of multiplication is important.

To multiply 2 matrices in MATLAB use * operator

 >> a = [1, 2 ; 3, 4]
      ans = 
        1     2
        3     4
 >> b = [3, 4 ; 5, 6];
 >> a * b
      ans =
        13    16
        29    36
 >> c = [3, 4, 5 ; 6, 7, 8];
 >> a * c
      ans =
        15    18    21
        33    40    47
        

1.2 Division

Division is more constrained than multiplication. In Algebra division is simple

LaTeX: a=bc
LaTeX: \frac{a}{c}=b

In Linear Algebra the order of the multiplication must be preserved. So

LaTeX: A=BC
LaTeX: AC^{-1}=BCC^{-1} \ne C^{-1}BC
and
LaTeX: B^{-1}A=B^{-1}BC=IC=C
where
LaTeX: I is the identity matrix.

In order to do division the matrix "in the denominator" must be invertable. Not all matrices are invertable. First and foremost the matrix must be square since LaTeX: A^{-1}A=I For those matrices that are not invertable the pseudoinverse can be used. In MATLAB the pseudoinverse can be found using the pinv command

 >> a = [1, 2 ; 3, 4];
 >> a^-1
      ans =
       -2.0000    1.0000
        1.5000   -0.5000
 >> pinv(a)
      ans =
       -2.0000    1.0000
        1.5000   -0.5000
These 2 operations lead to the same results since a is invertable. However, c is not
 >> c = [3, 4, 5 ; 6, 7, 8];
 >> c^-1
      ??? Error using ==> mpower
      Matrix must be square.
 >> pinv(c)
      ans =
       -1.2778    0.7778
       -0.1111    0.1111
        1.0556   -0.5556

See this sie on pseudoinverses for more information.

LaTeX: A^+=\left( A^T A \right)^{-1} A^T 'Pseudoinverse'




1.3 Squaring a matrix

Remember that squaring any value is the same as

LaTeX: X^2=X*X


In order to square a matrix in Linear Algebra the matrix must be square (same number of rows and columns) so that the inner dimensions are equal. Given a square matrix you can then take that matrix to some power in MATLAB using the ^ operator. For example

 >> a = [1, 2 ; 3, 4];
 >> a^2
      ans =
         7    10
        15    22

If, however, you wish to square the value in each element then use the . and ^ operators together

 >> a.^2
      ans =
        1     4
        9    16

The period before the ^ operator tells MATLAB to perform the operation element by element. The . operator works with division, multiplication, and other operators.

1.4 Determinant

To get the determinant of a matrix use the det command on that matrix, as follows

 >> a = [1, 2 ; 3, 4];
 >> det(a)
        ans = -2

1.5 Transpose

The transpose of a matrix is that matrix with the rows and columns swapped. To get the transpose of a matrix use the transpose command or the ' operator.

 >> a = [1, 2 ; 3, 4]
        ans = 
          1     2
          3     4 
 >> transpose(a)
        ans =
          1     3
          2     4
or
 >> a'
        ans =
          1     3
          2     4