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
  • 355,277
  • 75
  • 744
  • 836
r0u1i
  • 3,526
  • 6
  • 28
  • 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
  • 3,086
  • 4
  • 26
  • 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
  • 791
  • 3
  • 16