Is there anything like deal() for normal MATLAB arrays? [duplicate]

人盡茶涼 提交于 2019-11-26 14:49:16

问题


Possible Duplicate:
How do I do multiple assignment in MATLAB?

When dealing with cell arrays, I can use the deal() function to assign cells to output variables, such as:

[a, b, c] = deal(myCell{:});

or just:

[a, b, c] = myCell{:};

I would like to do the same thing for a simple array, such as:

myArray = [1, 2, 3];
[a, b, c] = deal(myArray(:));

But this doesn't work. What's the alternative?


回答1:


One option is to convert your array to a cell array first using NUM2CELL:

myArray = [1, 2, 3];
cArray = num2cell(myArray);
[a, b, c] = cArray{:};

As you note, you don't even need to use DEAL to distribute the cell contents.




回答2:


Not terribly pretty, but:

myArray = 1:3;
c = arrayfun(@(x) x, myArray , 'UniformOutput', false); 
c{:}


来源:https://stackoverflow.com/questions/2740704/is-there-anything-like-deal-for-normal-matlab-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!