Contents

Introduction to Loops

When we write matlab code more complicated than just simple data manipulation, we will need to create scripts and functions to save ourselves the difficulty of typing.

% We create scripts either by clicking the white sheet icon (old versions)
% or giant yellow plus (new version) in the toolbar. Alternately, we can
% type
edit myscript

If matlab can't find the file myscript.m, it will ask you if you want to create it.

Digression

When matlab looks for a file, it does so by searching its path, just like an operating system. You can type

path

to see the matlab path. To add a directory to the path, use

addpath('~/mypath') %...or something like this

You can also do this interactively under File ... Set Path. Any directory you add to the path will be searched first, so be careful that you don't name two files the same thing and wind up finding the wrong one because it's located later in the path ("overshadowing").

Finally, if you want to know whether a file is in the current path, you can use which:

which myscript

Later on, we will be able to use the output of this command to free ourselves from a lot of the trickiness of navigating folders. The output of which is a string variable that can be fed into commands to change directories and load files.

Creating a script

Now, let's make a test script to add up all the integers from 1 to some number. Copy and paste the following into your new file:

%myscript.m
%adds all numbers up to a certain integer
%ALWAYS COMMENT YOUR CODE!!!

maxnum=1e8;
tic
total=0;
for ind=1:maxnum
    total=total+ind;
end
toc %how long has it been since we called tic?

You can run this code either by typing myscript at the command line or pressing the green play button on the toolbar.

We can also compare this to the "Matlab way" of doing things:

tic
total=cumsum(1:maxnum);
toc

Believe it or not, sometimes loops are faster!

Example: Standard Error of the Mean

Now let's try a statistical experiment. Generate a sample of 30 data points from a normal distribution of mean 1 and standard deviation 2:

N=30;
mu=1;
sig=2; %note that we always code these as variables; that way, code is flexible
samp=mu+sig*randn(N,1);

What are the sample mean and standard deviation?

mean(samp)
std(samp)

We can even plot this:

hist(samp)

So here's a statistics question: We know the sample mean is distributed about the true mean, but can we verify this computationally? Yes! repeat the above process of drawing a sample 10000 times:

niter=10000;
musamp=nan(niter,1);
sigsamp=nan(niter,1); %we "preallocate" these so matlab is faster

for ind=1:niter
    this_samp=mu+sig*randn(N,1); %a new random sample every time!
    musamp(ind)=mean(this_samp);
    sigsamp(ind)=std(this_samp);
end

Now, what is the mean of the sample means?

mean(musamp)
std(musamp)
sig/sqrt(N) %the standard deviation of the mean estimate is the sem!

Plot it:

nbins=100;
hist(musamp,nbins)

More general for loops:

More generally, the iterator variable for our for loops can be any row vector. For example:

for ind=1:7:52
    disp(num2str(ind))
end

and

for ind=[16 pi -48 1+3*1i]
    disp(num2str(ind))
end

work just fine.