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

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

spiffy, just to complete the picture, here's how to do that starting from a vector: mat2cell([5 6 7], 1, [1, 1, 1]){:}
is it really the best way to do this in octave ??
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.)

Comments

0

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

>> [a,b,c] = deal(5,6,7)
a = 5
b = 6
c = 7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.