Explanation

The size of the indicated variable or array appears to be changing with each loop iteration. Commonly, this message appears because an array is growing by assignment or concatenation. Growing an array by assignment or concatenation can be expensive. For large arrays, MATLAB must allocate a new block of memory and copy the older array contents to the new array as it makes each assignment. Programs that change a variable's size in this way can spend most of their run time in this inefficient activity.

For the same reasons, there is significant overhead in shrinking an array or in changing the size of a variable on each iteration.


Suggested Action

Consider preallocating a variable or array before entering the loop by using zeros, ones, cell, or a similar function. Preallocating avoids the need for MATLAB to copy the data from one array to another inside the loop. For examples of code that do and do not preallocate an array, see “Preallocating Arrays”.

When an array grows by assigning past the end of the array or by using concatenation, preallocation alone does not improve the performance. The code must also use explicit indices.

If you do not know the size of an array before the loop begins, preallocate it, and then if necessary, shrink it after the loop completes.

If any of the following conditions are true, it might be appropriate to suppress this message, as described in Adjust Code Analyzer Message Indicators and Messages: