The variable $var appears to change size on every loop iteration. Consider preallocating for speed. So sayeth Matlab. Let's try it: x_prealloc = cell(10000, 1); x_end = {}; x_append = {}; for n=1:10000 % variant 1: preallocate x_prealloc(n) = {42}; % variant 2: end+1 x_end(end+1) = {42}; % variant 3: append x_append = [x_append {42}]; end Which variant do you think is fastest? Unsurprisingly, preallocation is indeed faster than growing an array. What is surprising is that it is faster by a co...