How to substitute multiple values to the same size variables
Show older comments
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
More Answers (1)
KSSV
on 31 Jan 2017
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
Categories
Find more on Scope Variables and Generate Names in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!