Getting Started Previous page   Next Page

Preallocation

If you can't vectorize a piece of code, you can make your for loops go faster by preallocating any vectors or arrays in which output results are stored. For example, this code uses the function zeros to preallocate the vector created in the for loop. This makes the for loop execute significantly faster.

Without the preallocation in the previous example, the MATLAB interpreter enlarges the r vector by one element each time through the loop. Vector preallocation eliminates this step and results in faster execution.


Previous page  Vectorization Creating Graphical User Interfaces Next page

© 1994-2005 The MathWorks, Inc.