%PREALLO_EX Example of preallocating code within MATLAB.
clear all
x = -10:.001:10;

% First, don't preallocate.
disp('Without preallocation:')

tic
for i = 1:length(x)
   y(i) = sin(2*x(i))+cos(x(i));
end
toc

% Now, preallocate the same loop.
disp('With preallocation:')

tic
y_pre = zeros(size(x));
for i = 1:length(x)
   y_pre(i) = sin(2*x(i))+cos(x(i));
end
toc

plot(x,y)