1

I'm working on a matlab code where I have a lot of variables that need to be preallocated (each variable is 8760x1 double). The values are generated in a for loop:

a=zeros(8760,1);
b=zeros(8760,1);
(...)
for i=1:8760
a(i)=[some code];
b(i)=[some code];
(...)
end

However, seeing that I have a lot of these variables, I want to preallocate the parameters in another file (more clean).

preallocate.m

a=zeros(8760,1);
b=zeros(8760,1);
...

main.m

preallocate
for i=1:8760
a(i)=[some code];
b(i)=[some code];
(...)
end

Will preallocating in another matlab file be as efficient as doing it in the same file as the executing file? Other suggestions?

ROLF
  • 284
  • 2
  • 14
  • 2
    I don't see any problems with this approach. I often split my program into several scripts for clarity – Luis Mendo Mar 09 '15 at 12:14
  • 2
    As for other suggestions: `a(8760,1)=0;` may be a faster allocation method than `a=zeros(8760,1);` – Luis Mendo Mar 09 '15 at 12:44
  • @LuisMendo: We learned to use zeros(nRow,nCol) for preallocation when learning basic Matlab at school. However, I see that your method is faster (at least for defining the matrix. What is the difference between these two? – ROLF Mar 09 '15 at 15:16
  • 1
    @ROLF - Here's a post by Eitan T that compares how to initialize an array of zeros using a variety of methods: http://stackoverflow.com/a/14195309/3250829 - It was determined that `zeros(M,N)` has a lot of unnecessary overhead. Basically, a lot of extra operations are performed before finally creating that array of zeroes. Doing `a(8760,1) = 0` has been shown to be amongst the fastest to initialize memory that is set to 0. You can check out that thread where that answer is for a more detailed analysis. – rayryeng Mar 09 '15 at 16:36

2 Answers2

4

Yes.‏‏‏ ‏‏ ‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    quoting the [exemplary answer](http://stackoverflow.com/a/28411646/1714410) of [rayryeg](http://stackoverflow.com/users/3250829/rayryeng). – Shai Mar 09 '15 at 12:44
  • 1
    lmao. +1 for quoting me and your very succinct answer. – rayryeng Mar 09 '15 at 16:31
0

This is a valid approach, but you must make sure that your variables make it to the workspace of your main function, i.e. you should set up the preallocate.m such that:

[a, b] = preallocate

that way when it is called in the main function, your preallocations will be initialized.

  • 2
    My understanding is that `preallocate.m` is a script, not a function. So it doesn't return any outputs – Luis Mendo Mar 09 '15 at 12:45
  • This could be, I like to use functions so my answer immediately drew from my own style :P – HoneyBadger17 Mar 09 '15 at 12:48
  • 1
    preallocate.m is a script, not a function. Using a function would force me to define inputs (none) and outputs. Given a large number of variables, the list of outputs would be long. I don't see that defining it as a function would do me any good? – ROLF Mar 09 '15 at 14:58
  • That is very fair, my fault for not picking up on the many variables request, script away. – HoneyBadger17 Mar 09 '15 at 15:36