How to formulate elegantly and performant functions that depend on a lot of input variables

2 ビュー (過去 30 日間)
Hello,
I am not new to MATLAB. However, I have the following problem:
If I have a function e.g.:
output = f(in1,in2,in3,in4,in5,in6,...)
which depends on a lot of input variables.
How can I design it as elegant as possible but at the same time performant?
If I use a struct e.g.
input.in1
input.in2
input.in3
...
I can formulate the function nice and short:
output = f(input)
But the problem is that the variable naming is fixed for the input as well as in the function. Furthermore, calling values from a struct is slow, so in the function they have to be read first:
function [output] = f(input)
in1 = input.in1
in2 = input.in2
in3 = input.in3
...
% calculations with in1,in2,in3,...
end
I also know that you should not write a function with so many input variables, yet it happens.
How do you handle such cases?
  8 件のコメント
Adam Danz
Adam Danz 2021 年 2 月 5 日
Another on of my favorites: The opposite order of inputs 1 & 2 in boxplot vs boxchart.
Walter Roberson
Walter Roberson 2021 年 2 月 5 日
But the problem is that the variable naming is fixed for the input as well as in the function.
Though there is a sense in which name/value pairs involves "variable naming is fixed for the input as well as in the function", and people use name/value pairs all the time.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2021 年 1 月 23 日
編集済み: Adam Danz 2021 年 1 月 23 日
A set of inputs like the example you shared is not inelegent. Some may argue that it's more elegant than the alternatives. What is inelegent is the undescriptive function and variable names which I'm guessing is just for the example.
  • If all inputs are numeric scalars or arrays of the same size and class, you could require the user to concatentate the values into a single multidimensional array and then the function can index or deconstruct the array as needed, but that's more work if the inputs are typically indpendent variables to begin with. Curve fitting functions often require this syntax for the objective function.
  • Another common solution is to use a cell array that support mixed classes and sizes but that also requires more work for the user to set up. For example,
in = {1, struct('t',5), 'a', false};
out = f(in);
  • If some of the inputs are not required you could use a varargin to reduce the number of input variables declared within the function. That requires carefully unpacking them, of course.
  • If the goal is to keep the variable references tidy within the function, you can use the input parser which validates and stores all inputs within a structure inside the function. That structure is used instide the function instead of the input variable names. I often use this approach to tidy-up the code.

その他の回答 (3 件)

Divija Aleti
Divija Aleti 2021 年 1 月 25 日
Hi Ferdinand,
Have a look at the Accepted Answer (and the comment under it) for the question posed in the following link:
For more information, refer to the link given below:
Regards,
Divija
  2 件のコメント
Adam Danz
Adam Danz 2021 年 1 月 25 日
編集済み: Adam Danz 2021 年 1 月 25 日
The OP hasn't indicated that they want to name variables dynamically, though.
Ferdinand Breit
Ferdinand Breit 2021 年 1 月 26 日
Hello,
thanks for your answers and comments, then it seems there is no real solution without using strcuts or cells.
Knowing this is also very helpful. Thanks!

サインインしてコメントする。


David Correa
David Correa 2021 年 2 月 1 日
Usar variables globales podría ser una opción más eficiente que pasar una estructura
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 1 日
No, global variables are the slowest kind of variables that exist. Referencing a variable that is a parameter is the fastest kind of variable access.

サインインしてコメントする。


MATLAB WAN
MATLAB WAN 2021 年 2 月 2 日
I usually using cell。

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by