- Avoid using explicit loop if possible
%calculating the sum of the product of corresponding element in two row vectors A and B %bad example s = 0; for i=1:100 s = s + A(i)*B(i); end %good example s = A*B';
- Avoid increment memory allocation, instead, try allocate memory first
%bad example s = []; for i=1:10000 s = [s some_number]; end %good example s = zeros(1,10000) for i=1:10000 s(ii) = some_number; end
Tips for writing faster MatLab programs
16 sec read