I have a array for example [1,2,3,4]. I want to assign a variable to each number in the array such that a=1, b=2, c=3, and d=4. I know I can do each one separately but I want to know if it is possible to this in one line.

댓글 수: 1

Image Analyst
Image Analyst 2011년 9월 29일
Kind of reminded me of the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F though I'm not saying you don't have a valid reason for doing that - you may well have.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 28일

13 개 추천

It is not possible in one statement. It is possible in two statements, which you could combine on one line.
t = num2cell([1,2,3,4]);
[a,b,c,d] = deal(t{:});

댓글 수: 8

Fangjun Jiang
Fangjun Jiang 2011년 9월 29일
Or could work without deal. [a,b,c,d] = t{:}
Walter Roberson
Walter Roberson 2011년 9월 29일
I don't believe I've see that documented. Based upon the descriptions of what {:} does, it would be reasonable to interpret it as equivalent to
[a,b,c,d] = t{1}, t{2}, t{3}, t{4}
which would be an invalid assignment (not enough output arguments) followed by three implicit disp()
Fangjun Jiang
Fangjun Jiang 2011년 9월 29일
It is documented. See Example 3 at the bottom of this page.
http://www.mathworks.com/help/techdoc/ref/deal.html
Teja Muppirala
Teja Muppirala 2011년 9월 29일
편집: Walter Roberson 2023년 4월 15일
You COULD do it in one statement, but, you probably wouldn't want to...
X = [10 20 30 40];
[a,b,c,d] = subsref(num2cell(X),struct('type',{'{}'},'subs',{{1:4}}))
Edwin Fonkwe
Edwin Fonkwe 2018년 1월 1일
Hi Teja, I'm sorry if I sound rude, but this is a terrible implementation on Matlab!
Daniel Bridges
Daniel Bridges 2018년 1월 25일
Edwin, have you seen Mathematica? From the introductory documentation I've read, it seems Wolfram wants users to code like that from the beginning ...
t = num2cell([1,2,3,4]);
[a,b,c,d] = deal(t{:})
a = 1
b = 2
c = 3
d = 4
[a,b,c,d] = t{:}
a = 1
b = 2
c = 3
d = 4
Back in 2011 it was not possible in one statement, at least not without an inline subsref(). A few years ago, however, a hack became available:
[a,b,c,d] = struct('x', num2cell([1,2,3,4])).x
a = 1
b = 2
c = 3
d = 4

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Raziman Thottungal Valapu
Raziman Thottungal Valapu 2020년 10월 1일

7 개 추천

It is now possible to do this in one line cleanly
[a,b,c] = deal(1, "test", [1:5])
Gives me
a =
1
b =
"test"
c =
1 2 3 4 5
As expected

댓글 수: 1

Stephen23
Stephen23 2020년 10월 1일
@Raziman Thottungal Valapu: no, that is not what the question is about. The question specifies that there is only one input array, but your code uses multiple separate inputs to deal. Not the same thing at all.

댓글을 달려면 로그인하십시오.

Viktor
Viktor 2018년 9월 1일
편집: Viktor 2018년 9월 1일

4 개 추천

The one-liner I have been using is the following:
[a,b,c,d] = feval(@(x) x{:}, num2cell([1,2,3,4]));
(Don't claim it is my original idea, just felt it belongs to this thread. If it is posted elsewhere feel free to link it.)

댓글 수: 2

Additionally, you can replace num2cell with any cell array of the right size, even one containing multiple data types. For example,
[a,b,c,d] = feval(@(x) x{:}, {rand(2), 5, 'hello', pi});
a
a =
0.9502 0.4387
0.0344 0.3816
b
b =
5
c
c =
hello
d
d =
3.1416
Stephen23
Stephen23 2018년 9월 12일
편집: Stephen23 2018년 9월 12일
That is just subsref wrapped up in an anonymous function, which is then called by feval.
Here it is with subsref called directly:
>> [a,b,c,d] = subsref({rand(2), 5, 'hello', pi}, substruct('{}',{':'}))
a =
0.103676 0.814128
0.208758 0.092132
b = 5
c = hello
d = 3.1416

댓글을 달려면 로그인하십시오.

Jeff Miller
Jeff Miller 2018년 8월 15일

3 개 추천

matsplit does this.

댓글 수: 2

Stephen23
Stephen23 2018년 8월 15일
+1 neat
Brent F
Brent F 2021년 6월 19일
Much cleaner than deal(a(1), a(2), a(3), a(4), ...)

댓글을 달려면 로그인하십시오.

Dan Erwin
Dan Erwin 2023년 4월 14일
편집: Walter Roberson 2023년 4월 14일

0 개 추천

The thing we are trying to do is emulate the very useful Python idiom e.g.
a,b,c,d = [1,2,3,4]
In Matlab, if you create a function assignList.m as follows:
function varargout = assignList( inputArray )
for i = 1:length( inputArray)
varargout{i} = inputArray(i);
end
end
then you can do for example
[a,b,c,d] = assignList( [1,2,3,4] )

댓글 수: 4

Fangjun Jiang
Fangjun Jiang 2023년 4월 14일
deal or no deal in MATLAB can do this already.
Stephen23
Stephen23 2023년 4월 14일
편집: Stephen23 2023년 4월 14일
@Fangjun Jiang: how? Please show how DEAL can be used with one vector/array input as shown.
Fangjun Jiang
Fangjun Jiang 2023년 4월 14일
@Stephen23, not with array input directly. See my comments at the answer by @Walter Roberson. My point is that this funciton is uncessary. Plus, I can't resist the "Deal or No deal" pun, now that I realized it.
Dan Erwin
Dan Erwin 2023년 4월 15일
I would not have written my post if I had noticed the earlier answer which recommended the contributed function matsplit, which is similar to mine but is more flexible when using multidimensional arrays.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2011년 9월 28일

댓글:

2023년 4월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by