MATLAB: Pre allocation do not work …

bugmatricepre-allocate

Hi everyone,
I am a bit lost,
I ran a code this morning and the pre allocation do not work, it changes the size of my matrices with any reason,
So I did a small try like this :
n=3
x = zeros(1,n);
for ii=1:10
x( ii ) = ( ii );
end
%
Before I got an error because the size just exceeds… now it works, it changes the size of x, how to avoid that ?
Best regards,

Best Answer

Question is not clear. What exactly are you trying to do here?The pre allocation works fine. You can check it by putting breakpoints in your code. The loop simply overwrites the pre allocated matrix. If you want to stop the loop when the execution reaches the preallocated matrix size then use something like this:
n=3
x = zeros(1,n);
temp=length(x);
for ii=1:10
x( ii ) = ( ii );
if ii==temp
break
end
end
%
Related Question