%VECTOR_EX Example of vectorizing code within MATLAB.
clear all
x = -10:.001:10;
y = x;

% First, use a for-loop.
disp('Time taken in for-loop:')

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

% Now, vectorize the same loop.
disp('Time taken for vector operation:')

tic
y_vec = sin(2*x)+cos(x);
toc

plot(x,y)