0

I have a function that returns 2 things (e.g. a function value and a jacobian).

function [f_val, J_val] = f(x)
   f_val = x;
   J_val = [1 0; 0 1];%.....
end

What I want is a function to extract the first or the second output parameter. Clearly I can go [f_sol; J_sol] = f([1 1]) and then extract the appropriate value, but I need to do this inside of an anonymous function where that sort of assignment isn't possible.

Ugly Solution One can create the following function,

function out = output_part(i, f, varargin) %This calls it here, but I would rather not
    [all_out{1:nargout(f)}] = f(varargin{:});
    out = all_out{i};
end

And then call something like: output_part(2, @f, [ 0 1]) serving my purpose, but it is ugly. Better syntax would be output_part(2, f([0,1])) but I can't figure out how to write this. What we would really need is a way to have the multiple variable outputs of a function end up as variable inputs of the calling function. I can't figure out if that is possible.

Multiple Output Arguments: This is not duplicate of How can I index a MATLAB array returned by a function without first assigning it to a local variable? This is of the same nature, but multiple arguments are handled differently. Something a little more related is the kthout function in How to elegantly ignore some return values of a MATLAB function? which does something like my output_part above.

The problem here just to be that you cannot easily chain multiple outputs of a function into the multiple inputs of another function through function composition.

Community
  • 1
  • 1
jlperla
  • 201
  • 2
  • 8
  • Possible duplicate of [How can I index a MATLAB array returned by a function without first assigning it to a local variable?](http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it) – David Nov 10 '15 at 01:07
  • 1. why don't you include an additional input argument to indicate which output you want, first or the second? 2. [This](http://blogs.mathworks.com/loren/2007/01/31/multiple-outputs/) and [This](http://stackoverflow.com/q/7921133/1586200) should be useful to you. – Autonomous Nov 10 '15 at 01:25
  • @David Thanks. Alas, I don't think this is a duplicate as multiple return values are treated very different in matlab, and seem not to play nice with function compositions. – jlperla Nov 10 '15 at 03:57
  • @ParagS.Chandakkar Thanks. (1) Alas, I can't modify that original function. Obviously I can write wrappers around it that emulate what you are talking about, but they are ad-hoc and ugly . (2) Thanks for the link to: http://stackoverflow.com/questions/7921133/anonymous-functions-calling-functions-with-multiple-output-forms It is very similar, and it looks like the one I posted as the "ugly solution" is a generalization of the posted solution. The difference is that I can't just use nullary functions as they do. – jlperla Nov 10 '15 at 03:59
  • I don't know what prevents you from modifying the original function. Otherwise, I don't know any other solution apart from those I have already listed. – Autonomous Nov 10 '15 at 04:37
  • Think of library functions, such as the `find` in those other examples where don't control the source l. So there is no way to get the notation I am suggesting? Basically, what is needed is a way to chain multiple outputs of a function to variadic inputs of a function... Then the rest would be easy. – jlperla Nov 10 '15 at 04:43
  • I think sometimes readability is more important than conciseness. – Autonomous Nov 10 '15 at 05:35
  • Exactly. And the ugly solution is tough to read. Which is why I asked if there was some sort of way to get `output_part(2,f(x))` working...the easiest one to read. – jlperla Nov 10 '15 at 05:42
  • I'm missing why you have to have the output_part function where you pass in a number to get the first or second output of f? Why do you need this? – Matthew Gunn Nov 10 '15 at 06:00
  • Why can't you `[value, jacobian] = f(x)` and use whichever one you need? I'm struggling to think of a situation where only at RUNTIME will your program know whether it wants the function value or the Jacobian. If you know at compile time what is needed, just hard code it. I can't imagine why something like your outer_part function is necessary. – Matthew Gunn Nov 10 '15 at 06:23
  • There are many places this would come up. For example, see the example in http://stackoverflow.com/questions/7921133/anonymous-functions-calling-functions-with-multiple-output-forms But for me it is about using anonymous functions where I want to compose functions rather than forcing some ugly intermediate function with storing temporaries. This isn't about having dynamic changes in what output is selected. – jlperla Nov 10 '15 at 06:36

0 Answers0