Computational Intelligence, SS08
2 VO 442.070 + 1 RU 708.070 last updated:
Course Notes (Skriptum)
Online Tutorials
Introduction to Matlab
Neural Network Toolbox
OCR with ANNs
Adaptive Filters
VC dimension
Gaussian Statistics
PCA, ICA, Blind Source Separation
Hidden Markov Models
Mixtures of Gaussians
Automatic Speech Recognition
Practical Course Slides
Animated Algorithms
Interactive Tests
Key Definitions
Literature and Links


Programming with Matlab

Flow Control


As usual the if statement evaluates a logical expression and executes a group of statements when the expression is true. The optional elseif and else keywords provide for the execution of alternate groups of statements. An end keyword terminates the last group of statements. The groups of statements are delineated by the four keywords no braces or brackets are involved.
>> T = [1 1 0 0];
>> if T == 1
     disp('Vector of ones.')
   elseif T == 0
     disp('Vector of zeros.')
     disp('Vector of ones and zeros.')

Vector of ones and zeros.
Note that for two matrices A and B , A == B does not test if they are equal, it tests where they are equal; the result is another matrix of 0's and 1's showing element-by-element equality. Only if T == 1 is true for all elements the if clause is executed.

switch and case

The switch statement executes groups of statements based on the value of a variable or expression. The keywords case and otherwise delineate the groups. Only the first matching case is executed.
>> method = 'Bilinear';
>> switch method
     case {'linear','bilinear'}
       disp('Method is linear')
     case 'cubic'
       disp('Method is cubic')
       disp('Unknown method.')

Method is linear

for and while

The for loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements.
>> b = 0; y = hardlim(w*P+b);
>> for i=1:4
     if y(i) == 1

The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements.

continue and break

The continue statement passes control to the next iteration of the for or while loop in which it appears, skipping any remaining statements in the body of the loop. In nested loops, continue passes control to the next iteration of the for or while loop enclosing it.

The break statement lets you exit early from a for or while loop. In nested loops, break exits from the innermost loop only.

Scripts and Functions

Files that contain code in the Matlab language are called M-files. You create M-files using a text editor, then use them as you would any other Matlab function or command. You can organize them into other directories and personal toolboxes that you can add to your Matlab search path. There are two kinds of M-files:


Scripts do not accept input arguments or return output arguments. They can operate on existing data in the workspace, or they can create new data on which to operate. Although scripts do not return output arguments, any variables that they create remain in the workspace, to be used in subsequent computations. An example is the M-file matlab_intro.m.


Functions, which can accept input arguments and return output arguments. Functions operate on variables within their own workspace, separate from the workspace you access at the Matlab command prompt. As example see the file perc.m.
function w=perc(P,T)
% PERC(P,T) Train a threshold unit using the
%    Delta-Rule until a separating Hyperplane is found.

figure(1); clf;
The first line of a function M-file starts with the keyword function. It gives the function name and order of arguments. In this case, there are up to two input arguments and one output argument. The next several lines, up to the first blank or executable line, are comment lines that provide the help text. These lines are printed when you type
>>  help perc
The first line of the help text is the H1 line, which Matlab displays when you use the lookfor command or request help on a directory.

An aspect of Matlab functions that is not ordinarily found in other programming languages is a variable number of arguments. Not all listed input arguments have to specified during a function call, e.g.
>> r = rank(A);
>> r = rank(A,1.e-6);
are both valid commands. Within the body of the function, two quantities named nargin and nargout are available which tell you the number of input and output arguments involved in each particular use of the function.

If you want more than one function to share a single copy of a variable, simply declare the variable as global in all the functions, e.g. by typing
>> global P T
Do the same thing at the command line if you want the base workspace to access the variable. The global declaration must occur before the variable is actually used in a function.