Octave basics: How to assign variables from a vector
Asked Answered
L

3

9

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 ...

Lasonde answered 28/3, 2012 at 13:20 Comment(0)
T
6

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
Trice answered 20/6, 2012 at 13:24 Comment(2)
spiffy, just to complete the picture, here's how to do that starting from a vector: mat2cell([5 6 7], 1, [1, 1, 1]){:}Lasonde
is it really the best way to do this in octave ??Ashleyashli
B
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.)

Bedrabble answered 9/3, 2022 at 2:7 Comment(0)
C
0

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

>> [a,b,c] = deal(5,6,7)
a = 5
b = 6
c = 7
Cyndy answered 9/3, 2022 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.