Wednesday, 9 February 2011

Introduction to Matrices in Matlab

Introduction to Matrices in Matlab

A basic introduction to defining and manipulating matrices is given here. It is assumed that you know the basics on how to define and manipulate vectors using matlab.
  1. Defining Matrices
  2. Matrix Functions
  3. Matrix Operations

Defining Matrices

Defining a matrix is similar to defining a vector. To define a matrix, you can treat it like a column of row vectors (note that the spaces are required!):
>> A = [ 1 2 3; 3 4 5; 6 7 8]

A =

     1     2     3
     3     4     5
     6     7     8

You can also treat it like a row of column vectors:
>> B = [ [1 2 3]' [2 4 7]' [3 5 8]']

B =

     1     2     3
     2     4     5
     3     7     8

(Again, it is important to include the spaces.)
If you have been putting in variables through this and the tutorial on vectors, then you probably have a lot of variables defined. If you lose track of what variables you have defined, the whos command will let you know all of the variables you have in your work space.
>> whos
  Name      Size                   Bytes  Class

  A         3x3                       72  double array
  B         3x3                       72  double array
  v         1x5                       40  double array

Grand total is 23 elements using 184 bytes

We assume that you are doing this tutorial after completing the previous tutorial. The vector v was defined in the previous tutorial.
As mentioned before, the notation used by Matlab is the standard linear algebra notation you should have seen before. Matrix-vector multiplication can be easily done. You have to be careful, though, your matrices and vectors have to have the right size!
>> v = [0:2:8]

v =

     0     2     4     6     8

>> A*v(1:3)
??? Error using ==> *
Inner matrix dimensions must agree.

>> A*v(1:3)'

ans =

    16
    28
    46

Get used to seeing that particular error message! Once you start throwing matrices and vectors around, it is easy to forget the sizes of the things you have created.
You can work with different parts of a matrix, just as you can with vectors. Again, you have to be careful to make sure that the operation is legal.
>> A(1:2,3:4)
???  Index exceeds matrix dimensions.

>> A(1:2,2:3)

ans =

     2     3
     4     5

>> A(1:2,2:3)'

ans =

     2     4
     3     5

Matrix Functions

Once you are able to create and manipulate a matrix, you can perform many standard operations on it. For example, you can find the inverse of a matrix. You must be careful, however, since the operations are numerical manipulations done on digital computers. In the example, the matrix A is not a full matrix, but matlab's inverse routine will still return a matrix.
>> inv(A)

Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 4.565062e-18


ans =

   1.0e+15 *

   -2.7022    4.5036   -1.8014
    5.4043   -9.0072    3.6029
   -2.7022    4.5036   -1.8014

By the way, Matlab is case sensitive. This is another potential source of problems when you start building complicated algorithms.
>> inv(a)
??? Undefined function or variable a.

Other operations include finding an approximation to the eigen values of a matrix. There are two versions of this routine, one just finds the eigen values, the other finds both the eigen values and the eigen vectors. If you forget which one is which, you can get more information by typing help eig at the matlab prompt.
>> eig(A)

ans =

   14.0664
   -1.0664
    0.0000

>> [v,e] = eig(A)

v =

   -0.2656    0.7444   -0.4082
   -0.4912    0.1907    0.8165
   -0.8295   -0.6399   -0.4082


e =

   14.0664         0         0
         0   -1.0664         0
         0         0    0.0000

>> diag(e)

ans =

   14.0664
   -1.0664
    0.0000

Matrix Operations

There are also routines that let you find solutions to equations. For example, if Ax=b and you want to find x, a slow way to find x is to simply invert A and perform a left multiply on both sides (more on that later). It turns out that there are more efficient and more stable methods to do this (L/U decomposition with pivoting, for example). Matlab has special commands that will do this for you.
Before finding the approximations to linear systems, it is important to remember that if A and B are both matrices, then AB is not necessarily equal to BA. To distinguish the difference between solving systems that have a right or left multiply, Matlab uses two different operators, "/" and "\". Examples of their use are given below. It is left as an exercise for you to figure out which one is doing what.
>> v = [1 3 5]'

v =

     1
     3
     5

>> x = A\v

Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 4.565062e-18


x =

   1.0e+15 *

    1.8014
   -3.6029
    1.8014

>> x = B\v

x =

     2
     1
    -1

>> B*x

ans =

     1
     3
     5

>> x1 = v'/B

x1 =

    4.0000   -3.0000    1.0000


>> x1*B

ans =

    1.0000    3.0000    5.0000
Finally, sometimes you would like to clear all of your data and start over. You do this with the "clear" command. Be careful though, it does not ask you for a second opinion and its results arefinal.
>> clear

>> whos

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...