Another Brief Matlab Tutorial

Matlab stands for Matrix Laboratory, indicating that most of its commands operate on matrices. Matlab is an interpreted language. The following sample code displays a 3D plot of a helix (try that in C...):
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);

First Steps

On CoC linux or Sun machines, just type matlab to run the interactive environment.

To get help:
Use whos to show all variables, clear to empty the workspace, and quit to exit Matlab

Matrix and Vector Operations

This is how we can define a vector (1 dimensional matrix)
>> v=[1, 2, 3]
Matlab prints out the following
v =

     1     2     3

Similarly we can define a matrix
>> M= [ 1 2 3; 4 5 6; 7 8 9]
The result is:
M =

     1     2     3
     4     5     6
     7     8     9

If you want to suppress the MatLab output then you need to finish the line with semicolon ';'
M= [ 1 2 3; 4 5 6; 7 8 9];

To find the sum of the columns of M we can use the 'sum' function.
>> sum(M)

ans =

    12    15    18

To find the sum of all elements use 
>> sum(sum(M))

ans =

    45

Note that the 'sum' function takes both matrices and vectors as parameters. The inner 'sum' takes a matrix and outputs a vector which is the input for the outer 'sum'.

By default the output is assigned to the 'ans' variable unless we assign the output explicitly
>> s=sum(M)

s =

    12    15    18

>> result=s+v

result =

    13    17    21

Some operations can produce long output. Typing 'more on' will switch to paging mode similar to "| more" in UNIX. 'more off' reverts to normal output.

To transpose a vector or a matrix the operator is just '.
>> v'

ans =

     1
     2
     3

>> M'

ans =

     1     4     7
     2     5     8
     3     6     9

To perform a component-by-component operation on a vector or matrix prepend the arithmetic operator by a '.'
>> M .* M

ans =

     1     4     9
    16    25    36
    49    64    81

This squares each element of the matrix. The result is different from squaring the matrix as shown below.
>> M*M

ans =

    30    36    42
    66    81    96
   102   126   150

To get a subset of a matrix do the following
>> M11=M(2:3 , 2:3)

M11 =

     5     6
     8     9

To get all elements in a given dimension one can use ':'
>> A= M( :, 1:2)

A =

     1     2
     4     5
     7     8

Functions

Each function should be separated in a .m file and should have the same name as the file. The syntax for defining a function is:
function res= myfunction(A,b)
where A and b are parameters and res is the variable whose value is returned as a result from the function. myfunction is an arbitrary function name. An example for calling this function is res=myfunction(someA,myb);

Flow Control

IF Example
      a=2;
      b=3;
      if a >= b
        c=a+b;
      elseif a ~= b
        c=2*a*b;
      else
        c= 5*b;
      end
        
Note that 'not equal' is written ~= and not != as it is in C.

FOR Example
     m=5;
     n=5;
     for i=1:m
       for j=1:n
         A(i,j)=i+j;
       end
     end   
     A
 
Break is also defined and has the same meaning as in C.

WHILE Example
     a=0; fa= -Inf;
     b=3; fb=  Inf;
     while b-a > eps*b
        x= (a+b)/2;
        fx= x^3 -2*x -5;
        if fx == 0
           break;
        elseif sign(fx) == sign (fa)
           a=x; fa = fx;
        else
           b = x; fb = fx;
        end
     end
     x

'for' loops can often be avoided by rewriting the computation in matrix form. For example:
     x=0;
     for  i=1:31
         y(i)= sin(x);
         x=x+0.1;
     end

...can be rewritten as:
     x2=0:0.1:3;
     y2=sin(x2);

Cell Arrays

Matrices and vectors can have only elements of the same type. They also have fixed dimensions. To overcome this limitation cell arrays are introduced.
C=cell(3,2)
C{1,1}=[1,2];
C{1,2}=[3,4];

C{2,1}=[5 6;
        7 8];
C{2,2}=[ 9 10 11;
        12 13 14];

C{3,1}= ones(3);
C{3,2}='Hello';

To access an element type:
>> C{3,1}

ans =

     1     1     1
     1     1     1
     1     1     1


>> C{2,2}(1,3)

ans =

    11

Note the use of the two different kinds of parentheses. The first one '{}' accesses the cell array elements. The second one '()' accesses the entries of the matrix contained in the cell array.

Useful ways of indexing

One very useful tool for indexing into a matrix is find. For example, to find all the black pixels in a binary image, you might do something like this:
[R,C] = find(I == 0);

Differences to C

Related documents

This documents is based on the following web pages:


This tutorial was written for CS7641/4641 Machine Learning, Spring 2003.
Michael Kaess, last changed 2003-02-20