How to time? Get number of days (as a fraction too) since January 1st, 1970 with $\texttt{now()}$
format long % change default printing format to better visualize days
now() / 365.25
format % reset the default printing format to the default
Get a more useful time format with $\texttt{clock()}$
% return the current date in years, months, days, hours, minutes and seconds
clock()
% you can use that to time execution
t1 = clock();
A = pinv(rand(5e2, 5e2)); % invert a REALLY large matrix
t2 = clock()
elapsed = t2 - t1
el_sec = elapsed(end)
N = 10;
tic();
for i = 1:10
A = pinv(rand(5e2, 5e2));
end
toc(); % by itself, toc() prints the time since the last tic()
% use a reference to the tic to inform toc which one to measure
t_total = tic();
for i = 1:5
t_loop = tic();
A = pinv(rand(5e2, 5e2));
toc(t_loop);
end
toc(t_total);
% capture the output of toc to use as a number
N = 10;
t = tic();
for i = 1:N
A = pinv(rand(5e2, 5e2));
end
elaps = toc(t); % nothing printed when we capture the value
fprintf('Time elapsed was %e min, wait, seconds\n', elaps / N)
elaps / N
a = [1, 2, 3];
a(end + 5) = 2.0
a(end + 1) = 1.0
% you can resize arrays in Matlab
a = [1, 2, 3]
% add numbers to just past the end
a(4) = 2;
a(end + 1) = 3;
a % print a
% you can add numbers way past the end
% missing values will be 0
a(30) = 17;
a % print a
% this works too
a = [1, 2, 3];
a(end + 4) = 17;
a
% a as a container for while loops
a = []; % a is empty
c = 0.5;
i = 1;
while c^i > 1e-5
a(i) = c^i;
i = i + 1;
end
a
N = 1e5;
t_prealloc = tic();
a = ones(1, N);
i = 2;
while i <= N
a(i) = 1.25 * a(i - 1);
i = i + 1;
end
el_prealloc = toc(t_prealloc)
t_nopre = tic();
b = [1];
i = 2;
while i <= N
b(i) = 1.25 * b(i - 1);
i = i + 1;
end
el_nopre = toc(t_nopre)
el_nopre / el_prealloc
% Octave's not really optimized, it gives a more pronounced effect in Matlab
Avoiding for-loops by all means possible.
% let's sum a hundred thousand random numbers
N = 1e5;
t = tic();
a = 0;
for i = 1:1e5
a = a + rand();
end
toc(t);
t = tic();
a = sum(rand(1, 1e5));
toc(t);
T_loop = zeros(1, length(0:6));
j = 1;
for N = 10.^(0:6);
t = tic();
% start the computation
a = 0;
for i = 1:N
a = a + rand();
end
% end the computation
el_loop = toc(t);
T_loop(j) = el_loop;
j = j + 1;
end
T_vect = zeros(1, length(0:(length(T_loop) - 1)));
j = 1;
for N = 10.^(0:6);
t = tic();
% start the computation
a = sum(rand(1, N));
% end the computation
el_vect = toc(t);
T_vect(j) = el_vect;
j = j + 1;
end
figure();
plot(0:(length(T_loop) - 1), T_loop ./ T_vect, 'LineWidth', 7);
title('Speed-up when using vectorization', 'FontSize', 18);
xlabel('Number of random samples to sum', 'FontSize', 18)
ylabel('Speed-up (times)', 'FontSize', 18);
a = 2.^(1:10)
x = 1:10;
y = 10:-1:1;
x .* y
a = [2, 3, 5, 7, 9] ./ (5:-1:1)
a = [2, 3, 5]
a([3, 1, 1])
a = linspace(0, 20, 21);
% select every other a
a(1:2:end)
% reverse a
a(end:-2:1)
b = ones(1, 10);
b(1:2:end) = b(1:2:end) * -1
a = [1, 2, 3, 4, 5];
b = [-1, -2, -3, -4, -5];
c = zeros(1, length(a) + length(b));
c(1:2:end) = a;
c(2:2:end) = b;
c([2, 2])
c
format compact
T = zeros(3, 10);
a = randi(10, 1, 10)
T(1, :) = a;
% logical indices, same size as array, number 1 for true and 0 for false
idxs = (a > 5) & (a < 9)
T(2, :) = (a > 5) & (a < 9);
a(idxs) = Inf
T(3, :) = a;
T
Extensive example: particles moving randomly on a plane
N = 1e3;
moves_nb = 50;
x = zeros(1, N);
y = zeros(1, N);
t = tic();
for i = 1:moves_nb
for j = 1:N
move = randi(4);
if move == 1 % right
x(j) = x(j) + 1;
elseif move == 2 % down
y(j) = y(j) - 1;
elseif move == 3 % left
x(j) = x(j) - 1;
elseif move == 4 % up
y(j) = y(j) + 1;
end
end
end
toc(t);
figure();
plot(x, y, '.', 'MarkerSize', 20);
N = 1e3;
moves_nb = 50;
x = zeros(1, N);
y = zeros(1, N);
t = tic();
for i = 1:moves_nb
r = randi(4, 1, N);
x(r == 1) = x(r == 1) + 1; % right
y(r == 2) = y(r == 2) - 1; % down
x(r == 3) = x(r == 3) - 1; % left
y(r == 4) = y(r == 4) + 1; % up
end
toc(t);
figure();
plot(x, y, '.', 'MarkerSize', 20);
a = rand(1, 1e5);
mean(a)
sum(a)
std(a)
median(a)
[val, idx] = max(a)
[val, idx] = min(a)
N = 1e7;
a = 100.0 * 2.0 * (rand(1, N) - 0.5); % numbers between -100 and 100
function y = my_abs(x)
y = x;
y(y < 0.0) = -y(y < 0.0);
end
% time our function (vectorized no less)
t_own = tic();
b = my_abs(a);
el_own = toc(t_own)
% time the in-built one
t_inb = tic();
b = abs(a);
el_inb = toc(t_inb)
el_own / el_inb