0

Possible Duplicates:
How do I do multiple assignment in MATLAB?
Is there anything like deal() for normal MATLAB arrays?

I want to put values of a vector in 2 variables, but it doesn't work.

vec = [2 3];
[m n] = vec;

I expected:

m = 2

n = 3

But I got an error.

It's a syntax problem or I can't do that?

Community
  • 1
  • 1
tech-ref
  • 269
  • 1
  • 2
  • 9
  • 1
    in short: you can do it, but not like that. – Jonas Jan 12 '11 at 18:05
  • 1
    And a couple more duplicates (this seems to be a fairly common question): [Is there anything like deal() for normal MATLAB arrays?](http://stackoverflow.com/questions/2740704/is-there-anything-like-deal-for-normal-matlab-arrays), [MATLAB Easiest way to assign elements of a vector to individual variables.](http://stackoverflow.com/questions/2893356/matlab-easiest-way-to-assign-elements-of-a-vector-to-individual-variables). – gnovice Jan 12 '11 at 18:07

2 Answers2

3

There are many ways to assign values of a vector to different variables, but you cannot do it like that.

Easy way:

vec = [ 2 3 ];
m = vec(1);
n = vec(2);
O_O
  • 4,397
  • 18
  • 54
  • 69
0

Just another variation using an anonymous function.

vec = [2 3];
tuple = @(x) deal(x(1), x(2))
[m n] = tuple(vec)
zellus
  • 9,617
  • 5
  • 39
  • 56
  • 1
    While this is correct, this is probably way to advanced for the OP. I suspect this is more of a "How do I index into a matrix?" question. – MatlabDoug Jan 12 '11 at 18:43