One Multi Dimensional Array or Several Two Dimensional Arrays ?

1 view (last 30 days)
Hello everyone, I'm writing a code that includes calling back a specific variable several times, i used Matlab Profiler in order to determine the specific time for every part of the code and it was 95 % of the run time associated with calling back the variable values, Now the variable that's being called back is actually 9 variables or 9 arrays f1(i,j), f2(i,j) , f3 (i,j),... etc. where i,j are changing using a for loop in order to fill the arrays, So i have 9 arrays that are Two Dimensional,
My question is, Which is faster: 1) To run the code as it is using 9 Two Dimensional Arrays f1(i,j), f2(i,j), f3(i,j) ...etc. or 2) To use One Multi Dimensional Array f(i,j,1) , f(i,j,2) , f(i,j,3) ... etc.
Knowing that the functions f1,f2,f3.. etc. are not dependent on each other (can i run in parallel ?).
My Goal is to minimize the code run time.
Thanks in advance

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jul 2015
Vectorize to do the entire function at one time. If you need to, use ndgrid:
[I, J] = ndgrid(1:maximum_i, 1:maximum_j);
Mahmoud Sedahmed
Mahmoud Sedahmed on 18 Jul 2015
Thanks Walter, unless the form of multidimensional array is saving large run time i won't re-write the code, it would take a lot of time to re-write the code in the new form of multidimensional arrays, and i guess that won't help much. Although i would try vectorizing, Thanks a lot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!