1

Possible Duplicates:
MATLAB Easiest way to assign elements of a vector to individual variables.
How do I do multiple assignment in MATLAB?

If I have a matrix: A = [1, 5, 10], do I set a1 = A(1), b1 = B(1), etc. on one line? I want to do something like:

[a1 a2 a3] = Blah(A)
Community
  • 1
  • 1
Neil G
  • 32,138
  • 39
  • 156
  • 257
  • 2
    this question has been asked before multiple times: http://stackoverflow.com/questions/2337126/how-do-i-do-multiple-assignment-in-matlab , http://stackoverflow.com/questions/2893356/matlab-easiest-way-to-assign-elements-of-a-vector-to-individual-variables , http://stackoverflow.com/questions/2740704/is-there-anything-like-deal-for-normal-matlab-arrays – Amro Sep 18 '10 at 21:48

1 Answers1

2

Aside from the answers you can find in all the questions I linked to, here's yet another one-liner inspired by this @gnovice post using SUBSREF:

>> A = [1 5 10];
>> [x y z] = subsref(num2cell(A), struct('type','{}','subs',{{':'}}))
x =
     1
y =
     5
z =
    10

Basically its equivalent to: [x y z] = num2cell(A){:} (but thats non-valid syntax)

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454