How do I count the number of Functions in a Function Handle?

16 views (last 30 days)
I have defined a Function Handle with multiple functions. I want to find a way to count the number of functions I have defined in the Function Handle, similar to finding the columns of a matrix using size(mat, 1).
As of now, calling the Function Handle in the terminal simply gives me its definition and its type (function_handle). Its dimensions are 1x1 in the Workspace.
fun_hand = @(x)[mod(x, 5), mod(x, 3), x^3, floor(20/x)];
% fun_hand is the Function Handle that has 4 functions
% it takes an input x and passes it into each function, returning a row vector of the outputs
no_of_fun = something(fun_hand);
% no_of_fun should contain the value 4 after execution
% I want to find this "something" and if it doesn't exist,
% then I want to find a way to parse through the Function Handle
  2 Comments
Karamjit Singh Bedi
Karamjit Singh Bedi on 19 Aug 2022
I did consider using in-built functions but for my actual application, I might need a few mathematical functions of the form "1/x". But the answer you have provided worked perfectly, I wanted a way to count the number of functions without actually executing the Function Handle.

Sign in to comment.

Accepted Answer

dpb
dpb on 19 Aug 2022
Edited: dpb on 19 Aug 2022
With coding style constraints
fun_hand = @(x)[mod(x,5);mod(x,3);x.^3;floor(20./x)].';
% the (constrained) engine
nFuncs=@(s)numel(strfind(func2str(fun_hand),';'))+1;
nFuncs(fun_hand)
ans = 4
The above limits what could be coded to not including any column vectors explicitly(*) as coefficients or other constants; that might, or might not, be something could live with.
There are probably(?) other artifices that could be dreamed up as well...
(*) That is, couldn't write [a;b;c] as a a coefficient term, but would have to use [a.',b.',c,'],' or an equivalent form instead.
  2 Comments

Sign in to comment.

More Answers (2)

Voss
Voss on 19 Aug 2022
Edited: Voss on 19 Aug 2022
It sounds like you actually want to count the number of elements in the vector returned by your function handle, in which case you can execute fun_hand with a scalar input and then use numel:
fun_hand = @(x)[mod(x, 5), mod(x, 3), x^3, floor(20/x)];
numel(fun_hand(0))
ans = 4
  4 Comments
Walter Roberson
Walter Roberson on 19 Aug 2022
'Yes, I was referring to "mathematical functions" and not the in-built Matlab functions.'
fun_hand = @(x)[mod(x, 5), mod(x, 3), x^3, floor(20/x)];
mod is one function, and it is being used twice here
^ is matrix power. You could argue that it is single function, but you could also argue that it is a lot of scalar multiplication and addition. The exact number of multiplication and additions required for x^2 is not certain... somewhere on the order of n^2.3 where n is the size of the matrix. But that optimized multiplication has a very high overhead and does not become cost effective until something like n=10^30 so nearly everyone uses n^3 in practice. As powers get higher you can reduce the work by binary decomposition of the power. Or you can flip over to decomposition, raise the diagonal to the power, reconstruct. It isn't clear what the appropriate mathematical function count should be.
/ is the matrix right division operator. Should the algorithm be smart enough to realize that the only time matrix power and matrix right division can be combined is for scalar x?
floor is a mathematical function
[] is considered a mathematical function, tuple formation.

Sign in to comment.


Walter Roberson
Walter Roberson on 18 Aug 2022
Edited: Walter Roberson on 18 Aug 2022
The number is always 1. Each function handle encodes exactly one function, which potentially returns a non-scalar output when executed. The part to be executed is always one expression, where the expression might happen to be the [] operator.
If you want to probe the size of the returned output, without executing the handle, you would have difficulty. For example @(n)repmat(42,1,max(2,min(3,n))) could return [42 42] or [42 42 42]. I speculate that your desired result here would be 1, same as @(n)[repmat(42,1,max(2,min(3,n)))] and I further speculate that you would want 2 returned for @(n)[repmat(42,1,max(1,min(2,n))), repmat(42,1,max(1,min(2,n)))] even though the possible return values are [42 42] or [42 42 42 42].
Your example function can return empty if the input is empty, or 1 x 4 if the input is scalar, but will error in the x^3 if the input is larger unless the input is a square matrix, and will error in 20/x if the input is larger and square.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!