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
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.
Yes, that works of course. But sometimes I might also want to modify the number of inputs. Hence I must always know the number of input arguments which might not always be the case.
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
Cam Salzberger 2017년 5월 17일
편집: Cam Salzberger 2017년 5월 17일

0 개 추천

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에 대해 자세히 알아보기

질문:

2017년 5월 17일

댓글:

2017년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by