Query output dimension of function handle
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Steven Lord
il 18 Mag 2017
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?
Risposte (1)
Cam Salzberger
il 17 Mag 2017
Modificato: Cam Salzberger
il 17 Mag 2017
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
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!