how to preallocate a variables
21 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Guillaume
2019 年 7 月 20 日
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
採用された回答
Guillaume
2019 年 7 月 21 日
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 件のコメント
Guillaume
2019 年 7 月 23 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!