Subcategory: None · Core · Parallel computing toolbox

Loop ordering

In Fortran, MATLAB, and R, arrays are stored in column-major order, meaning that the entries from the left-most index are stored contiguously in memory. This is often referred to as the left-most index varying quickest. This is different from many other popular scientific programming languages such as C, C++, and Python, which use a row-major ordering. The implication for writing loops is that it is always best to order loops by which indices vary quickest. In this way, the loop nest will traverse contiguous memory, which can be done efficiently.

Read More

Preallocation

When you grow an array dynamically inside a for loop, MATLAB must repeatedly allocate new memory and copy the existing contents. This can significantly degrade performance. Preallocating reserves sufficient contiguous memory upfront, avoiding costly resizing operations later.

Read More

Vectorisation

MATLAB is optimised to take advantage of vectorisation for mathematical operations between arrays, whereby the processor executes one instruction across multiple variables simultaneously. Vectorisation can perform mathematical operations many times faster than a for loop and even take advantage of conditional logic.

Read More

GPU Arrays

MATLAB makes it easy to run matrix operations on an NVIDIA GPU. There is a cost for moving data to and from the GPU, but if your entire algorithm can be done in matrix or vector operations, and your data is big enough, it can be faster.

Read More