如何在 MATLAB 中进行多重赋值?
这是我正在寻找的一个例子:
>> foo = [88, 12];
>> [x, y] = foo;
我期望之后会出现这样的情况:
>> x
x =
88
>> y
y =
12
但是我得到了这样的错误:
??? Too many output arguments.
我认为deal()
可能会这样做,但它似乎只对细胞有效。
>> [x, y] = deal(foo{:});
??? Cell contents reference from a non-cell array object.
我该如何解决我的问题?如果我想分别处理它们,我必须不断地用 1 和 2 进行索引吗?
Here's an example of what I'm looking for:
>> foo = [88, 12];
>> [x, y] = foo;
I'd expect something like this afterwards:
>> x
x =
88
>> y
y =
12
But instead I get errors like:
??? Too many output arguments.
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.
How do I solve my problem? Must I constantly index by 1 and 2 if I want to deal with them separately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您根本不需要
deal
(编辑:对于 Matlab 7.0 或更高版本),并且对于您的示例,您不需要mat2cell
;您可以使用num2cell
而不带其他参数::如果您出于其他原因想使用
deal
,您可以:You don't need
deal
at all (edit: for Matlab 7.0 or later) and, for your example, you don't needmat2cell
; you can usenum2cell
with no other arguments::If you want to use
deal
for some other reason, you can:请注意,
deal
接受“列表”作为参数,而不是元胞数组。因此,以下内容按预期工作:语法
c{:}
转换列表中的元胞数组,列表是 逗号分隔值,就像在函数参数中一样。这意味着您可以使用c{:}
语法作为deal
之外的其他函数的参数。要查看这一点,请尝试以下操作:Note that
deal
accepts a "list" as argument, not a cell array. So the following works as expected:The syntax
c{:}
transforms a cell array in a list, and a list is a comma separated values, like in function arguments. Meaning that you can use thec{:}
syntax as argument to other functions thandeal
. To see that, try the following:要在一行中使用
num2cell
解决方案,请定义一个辅助函数list
:To use the
num2cell
solution in one line, define a helper functionlist
:mtrw 所说的。基本上,您想使用 deal 处理元胞数组(尽管 deal(88,12) 也可以)。
假设您从 n×2 的数组 foo 开始,并且希望将第一列分配给 x,将第二列分配给 y,请执行以下操作:
What mtrw said. Basically, you want to use deal with a cell array (though deal(88,12) works as well).
Assuming you start with an array foo that is n-by-2, and you want to assign the first column to x and the second to y, you do the following:
DEAL 非常有用,但也非常令人困惑。我相信 foo 本身需要是一个元胞数组。以下似乎可以在 Octave 中运行,如果我没记错的话,它也可以在 MATLAB 中运行:
DEAL is really useful, and really confusing.
foo
needs to be a cell array itself, I believe. The following seems to work in Octave, if I remember correctly it will work in MATLAB as well:我无法评论其他答案,所以单独添加。
deal
也可以用作非标量的单行代码,当然,如果您已经将它们放在单独的变量中,那么:现在您用一行处理它们:
但是,如果它们打包在一个变量中,则只能在单元格或结构体数组中
deal
它们 - 对于单元格数组使用deal(X{:})
和用于结构体数组的deal(S.field)
。 (在后一种情况下,仅处理一个字段,但来自数组中的所有结构。)使用 Matlab v.7+,您可以使用X{:}
和S.field
没有deal
,如其他答案中所述。I cannot comment other answers, so separate addition.
deal
can be used as a one-liner for non-scalars as well, of course if you already have them in separate variables, say:and now you deal them with one line:
However, if they are packed in one variable, you can only
deal
them if they are in a cell or structure array - withdeal(X{:})
for cell array anddeal(S.field)
for structure array. (In the latter case only one field is dealt, but from all structures in array.) With Matlab v.7+ you can useX{:}
andS.field
withoutdeal
, as noted in other answers.为了方便起见,创建一个函数 arr2vars
你可以像这样使用它
Create a function arr2vars for convenience
You can use it then like this
您可能正在寻找
结果“
所以您有一条有效的单行线”。
You might be looking for
resulting in
So you have a working one-liner.
有一个更简单的方法。
在 Mathworks 中提供
完整文档。< /a>
There is an easier way.
Provides
Full documentation at Mathworks.