通缉:返回超过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.
<一个href=\"http://www.mathworks.co.uk/help/techdoc/matlab_prog/f4-70115.html#f4-71162\">http://www.mathworks.co.uk/help/techdoc/matlab_prog/f4-70115.html#f4-71162
什么是语法[在单行在这个职位的顶部 ,如code示例]?
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屋!