How to define several variables from vector at the same time
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (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
le 11 Oct 2017
[c, d, e] = deal(x(1), x(2), x(3));
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!