Preallocate — Instead of continuously resizing arrays, consider preallocating the maximum amount of space required for an array. For more information, see Preallocation.

Vectorize — Instead of writing loop-based code, consider using MATLAB matrix and vector operations. For more information, see Vectorization.

Place independent operations outside loops — If code does not evaluate differently with each for or while loop iteration, move it outside of the loop to avoid redundant computations.

Create new variables if data type changes — Create a new variable rather than assigning data of a different type to an existing variable. Changing the class or array shape of an existing variable takes extra time to process.

Use short-circuit operators — Use short-circuiting logical operators, && and || when possible. Short-circuiting is more efficient because MATLAB evaluates the second operand only when the result is not fully determined by the first operand. For more information, see Logical Operators: Short Circuit .

Avoid global variables — Minimizing the use of global variables is a good programming practice, and global variables can decrease performance of your MATLAB code.

Avoid overloading built-ins — Avoid overloading built-in functions on any standard MATLAB data classes.