Why the matlab editor (Code Analyser) is not warning about changing size by a variable within a loop

7 views (last 30 days)
In the 2nd example of the following for-loop the variable A definitely changes its size as it does in the 1st example. However, in one case the editor throws a warning message and in the second case it does not. Why?
A = [];
N = 100;
% 1st example
for k = 1:N
M = 10;
for n = M:-1:1
B(n) = n;
end
A = [A,B]; % Warning: Variable appears to change size on every loop iteration
end
% 2nd example
for k = 1:N
M = 10;
for n = M:-1:1
B(n) = n;
end
A(end+1:end+length(B)) = B; % No warning here and faster
end
If I write in simpler way
A = 1; % or A=[];
for k = 1:N
A(end+1:end+2) = [1,1]; % No warning.
end
And, by the way
A = 1; % or A = [];
for k = 1:N
A = [A,[]]; % Warning: Variable 'A' appears to change size on every loop iteration. Consider preallocating for speed
end
That means Code Analyser is not analysing much here
  3 Comments
G A
G A on 30 Dec 2021
Edited: G A on 30 Dec 2021
Rik - thanks for your checking! Some time ago I found some difference but your result shows no difference. How about the following differences?
timeit(@fun1);timeit(@fun2);% warm up online run tool
timeit(@fun1),timeit(@fun2) % actual timing runs
ans = 0.0019
ans = 9.1142e-04
function A=fun1
A = [];
for k = 1:1000
for n = 100:-1:1
B(n) = n;
end
A = [A,B.'];
end
end
function A=fun2
A = [];
for k = 1:1000
for n = 100:-1:1
B(n) = n;
end
A(:,end+1) = B.';
end
end
G A
G A on 30 Dec 2021
For cell array, the result is opposit
timeit(@fun4);timeit(@fun5);% warm up online run tool
timeit(@fun4),timeit(@fun5) % actual timing runs
ans = 0.0016
ans = 0.0025
function A=fun4
A = {};
for k = 1:1000
for n = 100:-1:1
B(n) = n;
end
A = {A,B};
end
end
function A=fun5
A = {};
for k = 1:1000
for n = 100:-1:1
B(n) = n;
end
A(end+1) = {B};
end
end

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 30 Dec 2021
For the same reason you get no warning about B growing inside a loop - MATLAB doesn't know what the index will be until the code is run, so it can't determine with certainty that the variable will be changing size every loop.
  3 Comments
Cris LaPierre
Cris LaPierre on 30 Dec 2021
Edited: Cris LaPierre on 30 Dec 2021
The exact behavior of the code analyzer is not documented, so there is no way to say for certain what it is doing. Use the warnings to learn best practices, and have that help shape how you write your code.
For your second question, when you do not preallocate, MATLAB picks any spot in memory to create the variable. As your variable grows with each loop, the new values are added to contiguous blocks of memory. If at some point your variable runs out of room in the place in memory where it was created, the entire variable gets copied to a new location in the memory with more space. The variable will continue growing until it too runs out of space, at which time it is again moved to a new place in memory.
That's a rough summary. You can read more here. You might also find this MathWorks blog post 'Understanding Array Preallocation' helpful.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!