% speeding up Matlab code by vectorizing.   barnett 11/2/07

N = 1e6;

% version 1: matlab doesn't know size of x so has to increase it by 1
% each time around the loop
clear x;
tic;
for n=1:N
  x(n) = sin(n);
end
toc

% version 2: preallocated memory but hand-written loop
x = zeros(1,N);                    % allocate memory block of the right size
tic;
for n=1:N
  x(n) = sin(n);
end
toc

% version 3: matlab does the loop (using its compiled code)
tic;
ns = 1:N;                % make a list of n values you want to process
x = sin(ns);             % hit them with the sin function simultaneously
toc

% Qu: why can't you vectorize the iteration of a map?
% (since need to wait for previous x(n) to be ready before find x(n+1)

% However: you can apply a map to a whole bunch of different
% initial conditions simultaneously, see julia.m and explormandel.m codes

