Query output dimension of function handle
이전 댓글 표시
Suppose I have the following two function handles:
f1 = @(x,y)[x+y, x*y];
f2 = @(x,y)[1,x,y,x^2,x*y, y^2]
Is there a way to query the dimension of the output of a specific function handle, i.e. in my example I want to get the answer "2" for f1 and the answer "6" for f2, since the first one maps to R^2 and the second to R^6.
Thanks in advance!
댓글 수: 3
Cyril GADAL
2017년 5월 17일
I don't know if I'm super usefull here, but I would make a calculation for one couple (x,y), and then look at the size of the result.
Lukas-Benedikt Fiechtner
2017년 5월 17일
Steven Lord
2017년 5월 18일
Let's say there was a function, call it sizeOfOutput, that accepts an anonymous function and returned the size of its output arguments (without executing that anonymous function.) What would you expect it to return for this?
f = @(n) zeros(n);
Or this?
g = @(n) zeros(randi([1 n], 2));
Or how about an anonymous function that accepts a file name and returns data read from that file?
답변 (1개)
Cam Salzberger
2017년 5월 17일
편집: Cam Salzberger
2017년 5월 17일
This is a good question, but it doesn't quite work with the way that MATLAB defines arrays or anonymous functions. Your question is assuming that you are inputting scalars into the anonymous function, which your code may very well do. However, MATLAB only knows about the function handle. For example, if I take the simple function:
f = @(x)[x,2*x];
and then call it with:
f(ones(1,2))
I'll get a 1x4 array output. So there's really no way to know what the output will be in these types of cases without knowing the input, and running the function on the input to see what happens.
Your comment, however, mentions that you are looking to modify the number of input arguments. That is pretty simple, you can just use "nargin":
nargin(f1)
This will tell you how many input arguments the anonymous function is expecting.
-Cam
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!