How to remove pre-allocation warning in this situation
allocation, matlab
Solution
You can try iterate in reverse order to avoid the warning:
for ei=length(E):-1:1,
hnds(ei) = plot(1:nP, avgR(ei,:), [clrStr(ei),'-'] );
end
In this case you do not need to pre-allocate (i.e., no `hnds = zeros(1,length(E));`).
By iterating in reverse order, the array size `hnds` is determined in the first iteration and stays fixed throughout the iterations.
See this thread for more information.
Problem
When plotting a set of figures using a for loop, for example: ``` for ei=1:length(E), hnds(ei) = plot(1:nP, avgR(ei,:), [clrStr(ei),'-'] ); end ``` There is a (the famous) warning in the code for the hnds(ei) variable: The variable hnds(ei) appears to change size on every loop iteration. Consider pre-allocating for speed. But when I try to pre-allocate the variable: ``` hnds = zeros(1,length(E)); ``` there is another warning for this new line and in the details for pre-allocation it says: Suggested Action: Avoid preallocating memory to a variable assigned to the output of another function. Is there any way to remove this warning, or should just ignore it?