Why are the computations slowing down in a for (or parfor)-loop that fills up an array of transfer function objects?

2 views (last 30 days)
It is sought to create an array of transfer functions that is being filled up with transfer functions within a loop, see the following code snippet,
 
for i = 1:n
tic
for j = 1:m
% .. some code
array_of_transfer_functions(i,j) = tf(nominator, denominator);
% .. some code
end
toc
end
It is noticed that the computations of the inner parfor-loop (the behavior is the same for a parfor loop) slow down with the time.
Why are the computations slowing down in a for (or parfor)-loop that fills up an array of transfer function objects?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Apr 2021
Edited: MathWorks Support Team on 15 Apr 2021
When creating an array of transfer function objects, then the system gets simply more complex rather than having in fact an array of independent transfer functions.
As in this case the array of transfer functions is not pre-initialized, the array representing an array of transfer functions gets more complex as the iterations progress, leading eventually to ever slower computational times.
In case it is sought to have an array of independent transfer functions, please use a cell array to store them instead, see the following code snippet,
 
array_of_transfer_functions = cell(n, m);
for i = 1:n
tic
for j = 1:m
% .. some code
array_of_transfer_functions{i, j} = tf(nominator, denominator);
% .. some code
end
toc
end
Note that preallocating the array before the for-loop is essential for performance reasons, as whenever an array is not preallocated, then MATLAB resizes the array in each iteration. For more information, please visit the following documentation page,

More Answers (0)

Categories

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

Tags

No tags entered yet.

Products


Release

R2012b

Community Treasure Hunt

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

Start Hunting!