需要:返回超过 1 个输出的匿名函数的 Matlab 示例 [英] Wanted: Matlab example of an anonymous function returning more than 1 output
问题描述
我使用匿名函数进行简单的数据值转换.匿名函数使用以下语法定义
I use anonymous functions for simple data value transforms. The anonymous functions are defined with the following syntax
sqr = @(x) x.^2;
我想要一个简单的匿名函数,它可以返回多个输出,如下所示...
I would like to have a simple anonymous function that returns more than one output that can be used as follows . . .
[b,a] = myAnonymousFunc(x);
Matlab 文档表明这是可能的,但它没有给出定义此类函数所需的语法示例.
The Matlab documentation suggests that this is possible, but it does not give an example of the syntax needed to define such a function.
http://www.mathworks.co.uk/help/techdoc/matlab_prog/f4-70115.html#f4-71162
定义这样一个函数的语法是什么[在一行中,就像我帖子顶部的代码示例]?
What is the syntax to define such a function [in a single line, like the code example at the top of my post]?
推荐答案
这能满足你的需要吗?
>> f = @(x)deal(x.^2,x.^3);
>> [a,b]=f(3)
a =
9
b =
27
在这个例子中,你需要确保你调用f
时只有两个输出参数,否则会出错.
With this example, you need to ensure that you only call f
with exactly two output arguments, otherwise it will error.
编辑
至少对于最新版本的 MATLAB,您只能使用 ~
语法返回部分输出参数:
At least with recent versions of MATLAB, you can return only some of the output arguments using the ~
syntax:
>> [a,~]=f(3)
a =
9
>> [~,b]=f(3)
b =
27
这篇关于需要:返回超过 1 个输出的匿名函数的 Matlab 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!