How to formulate elegantly and performant functions that depend on a lot of input variables
이전 댓글 표시
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
Bjorn Gustavsson
2021년 1월 23일
Sometimes a function requires a large number of input arguments. Then it is just to accept ones fate and program it that way. When it comes to options-type arguments I often combine them into a single struct. I find that neater than the "name-value" system used in matlab. But that is partly another question, and a matter of taste.
A large number of input arguments increases the risk of mixing up the inputs when calling the function. I have answered questions on this forum where bugs were caused by exactly this problem: too many input arguments, changes to the function, OP lost track of the inputs, no warnings or errors, surprisingly difficult to track down. How descriptive the names are is irrelevant for this to occur.
Use a structure. Acessing data from the structure is unlikely to be the slowest part of your code.
If after billions/trillions of iterations your code runs five minutes slower accessing data from a structure but you save yourself hours of debugging time, then it is still a net gain for you. Summary: write code that is reliable, this is a better use of your time.
lk
2021년 2월 5일
To mitigate the problem that Stephen Cobeldick mentions, namely
Stephen Cobeldick on 24 Jan 2021 at 6:35
... OP lost track of the inputs, no warnings or errors, surprisingly difficult to track down. ...
You could try (if you haven’t already) function argument validation introduced in R2020b. In particular, it will provide specific error messages for invalid inputs.
Here is the doc link
https://www.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html
Adam Danz
2021년 2월 5일
To add to lk's comment, for releases prior to 20b check out input parser and other input validation techniques.
Stephen23
2021년 2월 5일
@lk: that works if the data are distinguishable. A whole lot of scalar numeric parameters may be completely indistiguishable by value or range if swapped around... you can find threads on this forum where this has occurred.
Bjorn Gustavsson
2021년 2월 5일
@Stephen: In the case of "forgetting order" one has the help-section. If my head is really on the line I make sure to check even the order of inputs to atan2.
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.
채택된 답변
추가 답변 (3개)
Divija Aleti
2021년 1월 25일
0 개 추천
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
David Correa
2021년 2월 1일
0 개 추천
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.
MATLAB WAN
2021년 2월 2일
0 개 추천
I usually using cell。
카테고리
도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!