MATLAB Answers

Pre allocation do not work ...

1 view (last 30 days)
Gauthier Briere
Gauthier Briere on 3 Feb 2020
Commented: Raj on 3 Feb 2020
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,

  1 Comment

Adam
Adam on 3 Feb 2020
Pre-size it correctly! You pre-size it to 3 then put it in a for loop up to 10 assignging values to it. Where do you expect the next 7 elements to come from? You didn't pre-allocate those so the array grows in the loop.

Sign in to comment.

Accepted Answer

Raj
Raj on 3 Feb 2020
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
%

  4 Comments

Show 1 older comment
Raj
Raj on 3 Feb 2020
Can you please explain what you meant by "consistent results"? My understanding is that it doesn't really matter which one you use for a 1xN matrix other than maybe speed of execution in case of very large matrix.
Rik
Rik on 3 Feb 2020
For 1xN it doesn't matter, but as soon as your input is not a vector you will get into trouble. As you can see in the 'answer' below, x is actually an array. If you want to do something with all elements, use numel, otherwise you should use size with the second argument. If the user supplies a column vector and your code expects a row vector, the result could be a memory error. How should your user understand that?
bigN=100000;
A=rand(bigN,1);
B1=rand(bigN,1);
B2=B1';%user didn't read the doc and supplies the wrong shape
no_crash=A.*B1;
crash=A.*B2;
Raj
Raj on 3 Feb 2020
Thanks for the explanation!

Sign in to comment.

More Answers (1)

Gauthier Briere
Gauthier Briere on 3 Feb 2020
That's my point ! before I got normally an error saying that the indices exceed the size of the matrice.
If I do this example
n=3;
m=9;
x = zeros(n,m);
for ii=1:n
for jj=1:m
x(ii,jj) = ii;
end
end
and you change by :
n=3;
m=9;
x = zeros(n,m);
for ii=1:n
for jj=1:240151
x(ii,jj) = ii;
end
end
It will change the size of x which is really weird because before it never changed the size....

  3 Comments

Walter Roberson
Walter Roberson on 3 Feb 2020
It would give the index error if you tried to read from a location before writing. For example x(ii, jj) = x(ii, jj) + ii;
Raj
Raj on 3 Feb 2020
I still don't get it!! Why are you expecting an error? The preallocation simply helps in speeding up the loop. See this answer for details:
Other than that the for loop will always overwrite the preallocated loop. If you MUST HAVE an error you can insert it manually like this:
n=3
x = zeros(1,n);
temp=length(x);
for ii=1:10
if ii>temp
error('Index exceeds preallocated matrix size')
break
end
x( ii ) = ( ii );
end
%
Gauthier Briere
Gauthier Briere on 3 Feb 2020
Ok I think I got it, thanks for your help, I just misunderstood

Sign in to comment.

Sign in to answer this question.