How do I do multiple assignment in MATLAB?

2019-01-01 07:15发布

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?

9条回答
残风、尘缘若梦
2楼-- · 2019-01-01 07:38

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{:});
查看更多
骚的不知所云
3楼-- · 2019-01-01 07:41

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
查看更多
几人难应
4楼-- · 2019-01-01 07:48

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.

查看更多
笑指拈花
5楼-- · 2019-01-01 07:50

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.

查看更多
无色无味的生活
6楼-- · 2019-01-01 07:55

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
查看更多
与风俱净
7楼-- · 2019-01-01 07:57

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
查看更多
登录 后发表回答