且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将一个函数的多个输出直接传递给另一个?

更新时间:2023-11-09 23:22:40

要回答标题中的问题:使用以下函数,可以将函数的多个输出重定向到另一个函数:

 函数varargout = redirect(source (源,目标,n_output_source,which_redirect_from_source,varargin)
%(重定向(源,目标,n_output_source,which_redirect_from_source,...)
%重定向函数(源)的输出到另一个函数)
%source:函数指针
%destination:函数指针
%n_output_source:源函数的输出数(选择***过载函数)
%which_redirect_from_source:被重定向
%varargin参数到源函数
output = cell(1,n_output_source);
[output {:}] = source(varargin {:});
varargout = cell(1,max(nargout,1));
[va rargout {:}] =目标(输出{which_redirect_from_source});
end

现在我们可以将它应用到示例中:

  [〜,R] =重定向(@ meshgrid,@ cart2pol,2,[1,2],-2:2,-2:2) 

这里,源函数有2个输出,我们希望将输出1和2从源重定向到目标。 -2:2是源函数的输入参数。






处理上述示例的其他方法:如果您可以使用GNU Octave,并且 bsxfun nthargout ,这是您的解决方案:

  R = bsxfun(@(a,b)nthargout(2,@ cart2pol,a,b),( -  2:2)' ,( -  2:2))

在Matlab中一个可能的解决方案是:

  [〜,R] = cart2pol(meshgrid(-2:2),transpose(meshgrid(-2:2)))

 函数R = cart2pol2(m)
[〜,R] = cart2pol(m,m')
结束
$ b cart2pol2(meshgrid(-2:2,-2:2) )


Let me elaborate with examples: We know how to easily combine functions with a single output:

a = sin(sqrt(8));

Now consider this example code containing two steps to calculate R, with X and Y as intermediate outputs.

[X, Y] = meshgrid(-2:2, -2:2);
[~, R] = cart2pol(X, Y);

In general is there a way to combine the two functions and get rid of intermediate outputs? For instance how can I write something similar to [~, R] = cart2pol(meshgrid(-2:2, -2:2)) that works same as the previous code?

Note: What makes my question different from this question is that in my case the outer function accepts multiple inputs. Therefore I cannot and do not want to combine the outputs of the first function into one cell array. I want them to be passed to the second function separately.

To answer the question in the title: Using the following function, it is possible to redirect multiple outputs of a function to another:

function varargout = redirect(source, destination, n_output_source, which_redirect_from_source, varargin)
%(redirect(source, destination, n_output_source, which_redirect_from_source,....)
%redirect output of a function (source) to input of another function(destination)
% source: function pointer
% destination: function pointer
% n_output_source: number of outputs of source function (to select best overload function)
% which_redirect_from_source: indices of outputs to be redirected
% varargin arguments to source function
    output = cell(1, n_output_source);
    [output{:}] = source(varargin{:});
    varargout = cell(1, max(nargout,1));
    [varargout{:}] = destination(output{which_redirect_from_source});
end

And now we can apply it to the example:

[~,R] = redirect(@meshgrid,@cart2pol, 2, [1, 2], -2:2, -2:2)

Here, source function has 2 outputs and we want to redirect outputs 1 and 2 from source to destination. -2:2 is the input argument of the source function.


Other way to deal with the mentioned example: If you can use GNU Octave, with bsxfun and nthargout this is your solution:

R = bsxfun(@(a,b) nthargout(2, @cart2pol,a,b),(-2:2)',(-2:2))

in Matlab a possible solution is:

[~, R] = cart2pol(meshgrid(-2:2), transpose(meshgrid(-2:2)))

or

function R = cart2pol2(m)
    [~,R] = cart2pol(m,m')
end

cart2pol2(meshgrid(-2:2, -2:2))