MATLAB: How to save memory: loops or copy paste

loopMATLABmemory

I am wondering how one can save precious RAM in order to be able to handle larger arrays or more complex stuff.
Which uses less memory: using a loop to repeat a code (e.g.) three times or to copy the respective code three times in a row?

Best Answer

The memory used by the code itself is insignificant, so looping vs copy-paste is not a memory concern. For maintainability and readability, a loop would be far preferable ... or vectorize the loop if possible.
If you are having memory issues we would need to see your code before we could offer specific advice on how to decrease memory usage. Vectorization can often bring both speed and memory benefits, but it depends on what the code is doing.