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

1 visualización (últimos 30 días)
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 15 de Abr. de 2021
Editada: MathWorks Support Team el 15 de Abr. de 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,

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos


Versión

R2012b

Community Treasure Hunt

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

Start Hunting!

Translated by