How to define several variables from vector at the same time
Show older comments
Hi
I am trying to define several variables at the same time. The data for each variable is saved in a vector. I have tried the following:
x = [1 2 3];
y = [4 7 9];
[a b] = [x(end) y(end)]
%Or alternatively
[c d e] = [x(1) x(2) x(3)]
When I try this I receive the "Too many output arguments." error. Is there some easy way to do this?
I realize I could use:
a = x(end)
b = y(end)
But I would like a cleaner method.
Regards
Accepted Answer
More Answers (2)
You can use cell arrays and get the comma separated lists,
xy = num2cell([x y])
[a b] = xy{1,[3 6]};
[c d e] = xy{1, 1:3};
Walter Roberson
on 11 Oct 2017
[c, d, e] = deal(x(1), x(2), x(3));
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!