如何在 MATLAB 中进行多重赋值? [英] How do I do multiple assignment in MATLAB?

查看:48
本文介绍了如何在 MATLAB 中进行多重赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我正在寻找的示例:

Here's an example of what I'm looking for:

>> foo = [88, 12];
>> [x, y] = foo;

我会期待这样的事情:

>> x

x =

    88

>> y

y =

    12

但是我得到了如下错误:

But instead I get errors like:

??? Too many output arguments.

我认为 deal() 可能会这样做,但它似乎只适用于单元格.

I thought deal() might do it, but it seems to only work on cells.

>> [x, y] = deal(foo{:});
??? Cell contents reference from a non-cell array object.

我该如何解决我的问题?如果我想分别处理它们,我必须不断地索引 1 和 2 吗?

How do I solve my problem? Must I constantly index by 1 and 2 if I want to deal with them separately?

推荐答案

您根本不需要 deal(对于 Matlab 7.0 或更高版本),例如,您不需要不需要mat2cell;你可以使用 num2cell 而没有其他参数::

You don't need deal at all (edit: for Matlab 7.0 or later) and, for your example, you don't need mat2cell; you can use num2cell with no other arguments::

foo = [88, 12];
fooCell = num2cell(foo);
[x y]=fooCell{:}

x =

    88


y =

    12

如果您出于其他原因想使用 deal,您可以:

If you want to use deal for some other reason, you can:

foo = [88, 12];
fooCell = num2cell(foo);
[x y]=deal(fooCell{:})

x =

    88


y =

    12

这篇关于如何在 MATLAB 中进行多重赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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