assigning values with []
Show older comments
[a,b]=[1,2]
Why doesn't this work? How to rewrite into what is easier and more succinct than a=1; b=2?
Just to respond to all the comments and answers, there's certain occasions where my suggested (wrong) syntax is easier than others, e.g. when parameters are defined in an array in 1 go.
2 Comments
Image Analyst
on 13 Apr 2025
Edited: Image Analyst
on 13 Apr 2025
More succint? 11 characters is more succint than 8 characters?
length('[a,b]=[1,2]')
length('a=1;b=2;')
So it's not shorter and easier I think is a matter of opinion.
Also I think your suggestion could be ambiguous. Does [a,b]=[1,2] mean a=1;b=2, OR might someone think it could possibly mean that both a AND b are equal to the vector [1,2], like a=[1,2];b=[1,2]. So I think the a=1;b=2 method is a lot more intuitive - there is no possibility of misunderstanding what that means. It's unambiguous and more succint.
feynman feynman
on 14 Apr 2025
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!