Evan Guthrie
Evan Guthrie

Reputation: 31

How do I assign a single value/matrix to multiple variables in MATLAB?

I am attempting to assign the 6x6 identity matrix to 21 variables. My code looks like this:

[S1,S2,S3,S4,S5,S6,L21,L31,L41,L51,L61,L32,L42,L52,L62,L43,L53,L63,L54,L64,L65] = eye(6);

I understand why this does not work, but I have not been able to find any way to do this in a single line of code, which I really ought to be able to do.

How would I do this in the least amount of code possible? A similar example would be:

[a,b,c,d] = 5

How can I assign multiple variables to the same value/matrix?

Upvotes: 3

Views: 238

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60780

You can assign the same value to many variables using deal:

[S1,S2,S3,S4,S5,S6] = deal(eye(6));

Upvotes: 6

Related Questions