how to preallocate a variables
Show older comments
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 Comments
Rik
on 20 Jul 2019
Each function has its own workspace, so you should try to preallocate inside each function separately. It is hard to give you specific advice because you removed so much of the context and code.
Guillaume
on 20 Jul 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
on 21 Jul 2019
Edited: Asliddin Komilov
on 21 Jul 2019
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!