如何在 MATLAB 中进行多重赋值?

发布于 2024-08-22 13:31:28 字数 495 浏览 6 评论 0原文

这是我正在寻找的一个例子:

>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

香草可樂 2024-08-29 13:31:28

您根本不需要 deal (编辑:对于 Matlab 7.0 或更高版本),并且对于您的示例,您不需要 mat2cell;您可以使用 num2cell 而不带其他参数::

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

x =

    88


y =

    12

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

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

x =

    88


y =

    12

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

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
一梦浮鱼 2024-08-29 13:31:28

请注意, deal 接受“列表”作为参数,而不是元胞数组。因此,以下内容按预期工作:

> [x,y] = deal(88,12)
x = 88

y = 12

语法 c{:} 转换列表中的元胞数组,列表是 逗号分隔值,就像在函数参数中一样。这意味着您可以使用 c{:} 语法作为 deal 之外的其他函数的参数。要查看这一点,请尝试以下操作:

> z = plus(1,2)
z = 3

> c = {1,2};
> z = plus(c{:});
z = 3

Note that deal accepts a "list" as argument, not a cell array. So the following works as expected:

> [x,y] = deal(88,12)
x = 88

y = 12

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 the c{:} syntax as argument to other functions than deal. To see that, try the following:

> z = plus(1,2)
z = 3

> c = {1,2};
> z = plus(c{:});
z = 3
赠佳期 2024-08-29 13:31:28

要在一行中使用 num2cell 解决方案,请定义一个辅助函数 list

function varargout = list(x)
% return matrix elements as separate output arguments
% example: [a1,a2,a3,a4] = list(1:4)

varargout = num2cell(x);

end

To use the num2cell solution in one line, define a helper function list:

function varargout = list(x)
% return matrix elements as separate output arguments
% example: [a1,a2,a3,a4] = list(1:4)

varargout = num2cell(x);

end
三生一梦 2024-08-29 13:31:28

mtrw 所说的。基本上,您想使用 deal 处理元胞数组(尽管 deal(88,12) 也可以)。

假设您从 n×2 的数组 foo 开始,并且希望将第一列分配给 x,将第二列分配给 y,请执行以下操作:

foo = [88,12;89,13;90,14];
%# divide the columns of foo into separate cells, i.e. do mat2cell(foo,3,[1,1])
fooCell = mat2cell(foo,size(foo,1),ones(size(foo,2),1));
[x,y] = deal(fooCell{:});

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:

foo = [88,12;89,13;90,14];
%# divide the columns of foo into separate cells, i.e. do mat2cell(foo,3,[1,1])
fooCell = mat2cell(foo,size(foo,1),ones(size(foo,2),1));
[x,y] = deal(fooCell{:});
小矜持 2024-08-29 13:31:28

DEAL 非常有用,但也非常令人困惑。我相信 foo 本身需要是一个元胞数组。以下似乎可以在 Octave 中运行,如果我没记错的话,它也可以在 MATLAB 中运行:

> foo = {88, 12}
foo =

{
  [1,1] =  88
  [1,2] =  12
}

> [x,y] = deal(foo{:})
x =  88
y =  12

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:

> foo = {88, 12}
foo =

{
  [1,1] =  88
  [1,2] =  12
}

> [x,y] = deal(foo{:})
x =  88
y =  12
云淡月浅 2024-08-29 13:31:28

我无法评论其他答案,所以单独添加。

如果您从标量开始

deal 也可以用作非标量的单行代码,当然,如果您已经将它们放在单独的变量中,那么:

a = 123;
b = rand(3);
c = {a, b};
d = struct('field','val')

现在您用一行处理它们:

>> [x,y,z,w] = deal(a,b,c,d)
x =
   123
y =
    0.6370    0.2165    0.6711
    0.2945    0.8803    0.2705
    0.7633    0.1537    0.0767
z = 
    [123]    [3x3 double]
w = 
    field: 'val'

但是,如果它们打包在一个变量中,则只能在单元格或结构体数组中deal它们 - 对于单元格数组使用deal(X{:})和用于结构体数组的deal(S.field)。 (在后一种情况下,仅处理一个字段,但来自数组中的所有结构。)使用 Matlab v.7+,您可以使用 X{:}S.field没有 deal,如其他答案中所述。

I cannot comment other answers, so separate addition.

you can use deal(88,12) if you are starting from scalars

deal can be used as a one-liner for non-scalars as well, of course if you already have them in separate variables, say:

a = 123;
b = rand(3);
c = {a, b};
d = struct('field','val')

and now you deal them with one line:

>> [x,y,z,w] = deal(a,b,c,d)
x =
   123
y =
    0.6370    0.2165    0.6711
    0.2945    0.8803    0.2705
    0.7633    0.1537    0.0767
z = 
    [123]    [3x3 double]
w = 
    field: 'val'

However, if they are packed in one variable, you can only deal them if they are in a cell or structure array - with deal(X{:}) for cell array and deal(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 use X{:} and S.field without deal, as noted in other answers.

狂之美人 2024-08-29 13:31:28

为了方便起见,创建一个函数 arr2vars

function varargout = arr2vars(arr)
% Distribute elements over variables

N = numel(arr);
if nargout ~= N
    error('Number of outputs does not match number of elements')
end
for k = 1:N
    varargout{k} = arr(k);
end

你可以像这样使用它

[~,roi] = imcrop(im);
[x,w,y,h] = arr2vars(roi);

Create a function arr2vars for convenience

function varargout = arr2vars(arr)
% Distribute elements over variables

N = numel(arr);
if nargout ~= N
    error('Number of outputs does not match number of elements')
end
for k = 1:N
    varargout{k} = arr(k);
end

You can use it then like this

[~,roi] = imcrop(im);
[x,w,y,h] = arr2vars(roi);
始终不够爱げ你 2024-08-29 13:31:28

您可能正在寻找

>>> foo = [88, 12];
>>> [x, y] = deal(foo(1), foo(2))

结果“

x = 
    88

y = 
    12

所以您有一条有效的单行线”。

You might be looking for

>>> foo = [88, 12];
>>> [x, y] = deal(foo(1), foo(2))

resulting in

x = 
    88

y = 
    12

So you have a working one-liner.

泛泛之交 2024-08-29 13:31:28

有一个更简单的方法。

x = foo (1, 1)  
y = foo (1, 2)

Mathworks 中提供

>> x

x =

88

>> y

y =

12

完整文档。< /a>

There is an easier way.

x = foo (1, 1)  
y = foo (1, 2)

Provides

>> x

x =

88

>> y

y =

12

Full documentation at Mathworks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文