how to preallocate a variables

22 visualizaciones (últimos 30 días)
Asliddin Komilov
Asliddin Komilov el 20 de Jul. de 2019
Comentada: Asliddin Komilov el 24 de Jul. de 2019
How do I preallocate variable in this code, when they are inside different functions? Thanks
In general it looks like this:
Script:
A=0:1300;
for i=1:10:length(B)
[]=function1()
end
Function1:
B=0:133;
for i=1:length(B)
[]=function2()
end
Function2:
C= xlsread()
D=1:129
for i=1:length(D)
Variables (c)
[]=function3(Variables (c))
end
  3 comentarios
Guillaume
Guillaume el 20 de Jul. de 2019
We're going to need more details. In the little snippets of code you show, nothing can be preallocated. Typical case of preallocation is when you assign to an indexed variable inside a loop:
for i = 1:100
A(i) = somefunction; %preallocation of A advised!
end
Asliddin Komilov
Asliddin Komilov el 21 de Jul. de 2019
Editada: Asliddin Komilov el 21 de Jul. de 2019
that is the catch, output variables in the most inner function come out as the output variables of the most outer function, indexes of the variables in different functions is different inside and outside of the functions, according to the eparchy. Preallocating all variables takes space, because they are not deleted after function runs. I hope this is now more understandable:
A=0:1300;
for i=1:10:length(A)
[m,n,c,D,B]=function1(A)
end
Function1:
B=0:133;
for i=1:length(B)
[m,n,c,D]=function2(A,B)
end
Function2:
C= xlsread()
D=1:129
for i=1:length(D)
Variables (c)
[m,n,c]=function3(Variables (A,B,D,c))
end

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 21 de Jul. de 2019
Again, with the code you show, there's nothing that can be preallocated. But your loops are also not very useful since the overwrite the output variable on each step. So, perhaps your question is not about preallocation but how to keep all the values returned by the loop (where indeed you would use preallocation to make the code more efficient).
In that case, it's simple you need to index the variables you assign to. Depending on the size/shape of the outputs your indexing is going to look different, so again, more details required.
Assuming the outputs are scalar:
steps = 1:10:1301;
%preallocation advised but not required:
m = zeros(1, numel(steps));
n = zeros(1, numel(steps));
c = zeros(1, numel(steps));
D = zeros(1, numel(steps));
B = zeros(1, numel(steps));
%or they can all be preallocated at once with:
[m, n, c, D, B] = deal(zeros(1, numel(steps)));
%looping
for i = steps
[m(i), n(i), c(i), D(i), B(i)] = function1(something);
end
  7 comentarios
Guillaume
Guillaume el 23 de Jul. de 2019
Each time you have a loop that does:
for i = 1:numiters
somevariable(:, :, i) = something; %may have more or less dimensions, doesn't matter
end
You must preallocate somevariable to avoid slow-downs. so:
somevariable = zeros(dim1, dim2, numiters);
for i = 1:numiters
somevariable(:, :, i) = something;
end
In case you're not aware of the reason for the preallocation, if you don't preallocate on the first iteration of the loop, matlab does:
somevariable(:, :, 1) = something;
somevariable doesn't exist, so matlab allocates enough room to fill somevariable, so allocates a dim1 x dim2 x 1 matrix. On the second iteration,
somevariable(:, :, 2) = something;
matlab sees that somevariable exist, and dim1 and dim2 are the correct size, but dim3 only goes up to 1 and now needs to go up to 2. Matlab need more room, so it allocates a new array of size dim1 x dim2 x 2, copy over the content of the previous array in (:, :, 1) and fill (:, :, 2) with the new result.
Rinse and repeat, each step of the loop, matlab needs to create a new array one size bigger than the previous step, copy over all the data from the previous steps and put the data from the current step.
If you tell matlab beforehand the final size of the array, matlab just have to fill the memory you've allocated. It's a lot faster.
Asliddin Komilov
Asliddin Komilov el 24 de Jul. de 2019
I thank you very much for your assistance, but somehow it did not help much, so I opened another question:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by