通缉:匿名函数返回超过 1 个输出的 Matlab 示例 [英] Wanted: Matlab example of an anonymous function returning more than 1 output

查看:36
本文介绍了通缉:匿名函数返回超过 1 个输出的 Matlab 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用匿名函数进行简单的数据值转换.匿名函数使用以下语法定义

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屋!

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