As you say, in Octave you should in general try to avoid for-loops in favor of vectoriztation.
Octave has kind of support for JIT-optimization as Matlab has, but in addition Octave
has many overlooked vectorization functions.
clear;close;clc;
M = 5e2;
nrTime = 2e2;
sampleTime = 5;
v_i_orig = rand (M, nrTime);
p_i_orig = rand (M, nrTime);
a_i = rand (M, nrTime);
v_i = v_i_orig;
p_i = p_i_orig;
tic
for i = 2:nrTime
v_i(:, i) = v_i(:, i-1) + a_i(:, i) * sampleTime; % euler integrator
p_i(:, i) = p_i(:, i-1) + v_i(:, i) * sampleTime; % euler integrator
end
t1 = toc;
v_i_ref = v_i;
p_i_ref = p_i;
v_i = v_i_orig;
p_i = p_i_orig;
tic
i = 2:nrTime;
v_i(:, i) = a_i(:, i) * sampleTime; % euler integrator
v_i = cumsum (v_i, 2);
p_i(:, i) = v_i(:, i) * sampleTime; % euler integrator
p_i = cumsum (p_i, 2);
t2 = toc;
% Ensure both computation result in the same result.
assert (isequal (v_i_ref, v_i))
assert (isequal (p_i_ref, p_i))
t1/t2