How to formulate elegantly and performant functions that depend on a lot of input variables
조회 수: 1 (최근 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
2021년 2월 5일
Another on of my favorites: The opposite order of inputs 1 & 2 in boxplot vs boxchart.
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
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.
댓글 수: 0
추가 답변 (3개)
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
David Correa
2021년 2월 1일
Usar variables globales podría ser una opción más eficiente que pasar una estructura
댓글 수: 1
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!