8

Maybe I'm spoiled by Python, but does Octave allows one to assign the values of variables directly from a vector? That is, doing something like

a,b,c=[5,6,7]

will result with a=5, b=6, c=7. I have tried many combinations of writing the expression above, but no luck yet ...

Fred Foo
  • 342,876
  • 71
  • 713
  • 819
r0u1i
  • 3,496
  • 6
  • 27
  • 36

3 Answers3

5

This can be done by constructing a cell array with "{...}" and converting this to a comma separated list via "{:}":

[a b c] = {5 6 7}{:}
a =  5
b =  6
c =  7
Christian
  • 66
  • 1
  • 2
0

This seems to work when you actually have a vector to deal with:

v = [5, 6, 7];
[a, b, c] = num2cell(v){:}

(Extracted from this Matlab answer.)

Ulf Åkerstedt
  • 2,916
  • 3
  • 25
  • 28
0

deal() is meant for this task, for separate input values:

>> [a,b,c] = deal(5,6,7)
a = 5
b = 6
c = 7
S. Gougeon
  • 438
  • 2
  • 15