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.

 Accepted Answer

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.

More Answers (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 Comment

@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

Sign in to comment.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Asked:

on 31 Jan 2017

Edited:

on 31 Jan 2017

Community Treasure Hunt

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

Start Hunting!