如何将一个函数的多个输出直接传递给另一个? [英] How to directly pass multiple outputs of a function to another?

查看:23
本文介绍了如何将一个函数的多个输出直接传递给另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我用示例详细说明:我们知道如何轻松地将函数与单个输出组合起来:

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

a = sin(sqrt(8));

现在考虑这个示例代码,它包含两个步骤来计算 R,其中 XY 作为中间输出.

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);

一般来说有没有办法结合这两个功能并摆脱中间输出?例如,我怎样才能写出类似于 [~, R] = car2pol(meshgrid(-2:2, -2:2)) 的东西,它的工作原理与之前的代码相同?

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)

这里,源函数有 2 个输出,我们希望将输出 1 和 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.

处理上述示例的其他方法:如果您可以使用 GNU Octave,以及 bsxfunnthargout,这就是您的解决方案:

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))

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

in Matlab a possible solution is:

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

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

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

这篇关于如何将一个函数的多个输出直接传递给另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆