Borrar filtros
Borrar filtros

help: efficiently pass a lot of variables to a function

15 visualizaciones (últimos 30 días)
Archit
Archit el 15 de Sept. de 2013
Hi,
I have a function which is called 10^5 times.
The problem is this function needs like 40 values (mostly scalars or small matrices) stored in variables to compute its result.
  • Either i have to pass these values as parameters to the function (which is impractical as i have around 30-40 variables that are needed)
  • Another thing i can do is is to store these variables in a struct and pass that struct in function. Then i can unpack the struct in the function workspace using 'v2struct' from FEX. This takes a LOT OF TIME to execute.
  • Third solution that i came up with which is equally as bad as second one is to save the variables in a mat file and load the mat file in the function.
The second method is terribly slow because of unpacking done in each call. (profiler shows this).
third method is also equally slow because of disk read-writes.
Note: it is important to have variable names associated with the data passed to the function, otherwise (if i pass a matrix or cell array) it would be impossible to keep track of the indices in the code.
Please give your suggestions how can this be achieved in a better and faster way
  3 comentarios
Archit
Archit el 16 de Sept. de 2013
just imagine the headache of writing everytime funct(var2,var2,var3,... ,var40)
Sean de Wolski
Sean de Wolski el 16 de Sept. de 2013
@Archit, you only have to write it once; then copy and paste it. I don't see how packing/unpacking etc will avoid these steps.
Do you actually have variables named var1,var2,var3...varn? If so, then I would consider changing how you do this.

Iniciar sesión para comentar.

Respuestas (3)

Jan
Jan el 16 de Sept. de 2013
Editada: Jan el 16 de Sept. de 2013
Using a struct to provide a set of inputs is very efficient, but unpacking is a problem because the dynamic creation of variables pollutes the lookup-table of the variables. Then a simple solution is to unpack the struct manually or not at all.
function Out = myFunc(S)
data = S.data; % manual unpacking
plot(S.x, S.y); % No unpacking
The unpacking is useful only, if the contents is processed repeatedly, e.g. in a loop.
[EDITED] Compared to your first method, this has the benefit that the order of the inputs cannot be confused easily, because the field names care for the correct relation.
For a larger program using 2 or 3 structs can be useful: One for the processing data, one containing user-interface related settings and the third e.g. for global parameters. Splitting the set of arguments into logical sections allows to use object oriented designs without the requirement to program with OO methods.
  2 comentarios
Archit
Archit el 16 de Sept. de 2013
tight now i am not unpacking. I am using my variables like S.var1, S.var2 and so on.
But i would appreciate if someone would give me a solution where var1, var2 would be sufficient
Jan
Jan el 16 de Sept. de 2013
Editada: Jan el 16 de Sept. de 2013
I do not understand, what "sufficient" means here. Do you want to use "var1" as a name of a variable? Then:
var1 = S.var1;
This looks such trivial, that I do not think, that I understand the underlying problem. Do you want to avoid this line? If so, why? Creating "var1" auto-magically reduces the efficiency of Matlab's JIT accelerator massive, such that I cannot imagine why this could be wanted.

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 15 de Sept. de 2013
put all your variables in a cell array. For example
Your_array={var1,var2,var3}
Then you can use one variable as input argument
function y=fcn(your_array)
var1=your_array{1}
var2=your_array{2}
var3=your_array{3}
  2 comentarios
Archit
Archit el 16 de Sept. de 2013
As i stated in by question
Note: it is important to have variable names associated with the data passed to the function, otherwise (if i pass a matrix or cell array) it would be impossible to keep track of the indices in the code.
Azzi Abdelmalek
Azzi Abdelmalek el 16 de Sept. de 2013
You do not need to create all those variable names, just work with your_array{1},your_array{2}...

Iniciar sesión para comentar.


Sean de Wolski
Sean de Wolski el 16 de Sept. de 2013
I wouldn't discount your first option so quickly. It'll take a minute or two to write that function signature then from there you're good to go!

Community Treasure Hunt

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

Start Hunting!

Translated by