How to substitute multiple values to the same size variables

I want to shorten multiple substitute equations shown below:
a = 1;
b = 2;
c = 3;
I have tried following expression but it did not work.
[a b c] = [1 2 3];
This may be because MATLAB recognize [ 1 2 3 ] as a single vector.
Does anyone know a proper way to shorten these multiply substitution ?
Thank you.
P.S. :
Thank you for your answering.
I know that it's better to handle variables with array, but I sometimes need to prioritize readability.

 采纳的回答

If you really want to do that then put the values into a cell array first:
>> X = {1,2,3};
>> [a,b,c] = X{:};
>> a
a = 1
>> b
b = 2
>> c
c = 3
The X{:} syntax generates a comma-separated list from the elements of X, which can be allocated to the variables on the LHS.
It is quite likely that this is not going to be an efficient use of MATLAB though. In general it is much more efficient to keep you data together as much as possible rather than splitting it apart. Keeping those values in one simple numeric vector may be the best solution.

更多回答(1 个)

You must proceed as follows:
a = [1 2 3] ;
then your a,b,c will be a(1),a(2),a(3) respectively.
Or
a = 1 ; b = 2 ; c = 3 ;
iwant = [a b c]

1 个评论

@Yoshi Ri: The pseudo code "[a b c] = [1 2 3]" looks like the three variables have a common nature. If so, storing them in an array could be the best option:
v = [1, 2, 3]
While Stephen's answer matchs your question more precisely, I assume, that KSSV's answer might be better to solve the underlying problem. +1

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by