0

I want to take the simple example code and assign a unique rand() to each variable -- on one line:

X = rand();

Y = rand();

possible?

Amro
  • 123,847
  • 25
  • 243
  • 454
j4ke
  • 3
  • 1
  • 2
    why wont you just use a vector ? `x=rand(1,2)` then `x(1)` and `x(2)` are your two variables... – bla Sep 13 '13 at 23:33
  • agreed - that would be cleaner here. for the larger problem, i needed a lot of variables with unique descriptive names – j4ke Sep 14 '13 at 03:12
  • possible duplicate of [How do I do multiple assignment in MATLAB?](http://stackoverflow.com/questions/2337126/how-do-i-do-multiple-assignment-in-matlab) – Eitan T Sep 15 '13 at 13:25

2 Answers2

2

If you absolutely want it in a single line, you can do:

[X, Y] = deal(rand(), rand());

Not a big gain, though.

Milo
  • 2,062
  • 16
  • 38
  • cool, that's the function i was looking for - thanks! (agreed, it's overkill / loses readability for this light code, but will help a great deal for my other implementations.) – j4ke Sep 14 '13 at 03:09
  • @j4ke / Milo, In MATLAB version 7.0 or higher, you can achieve the same effect without `deal` using [comma-separated lists](http://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html). Refer to [this answer](http://stackoverflow.com/q/2337126/1336150#2337336) of the duplicate question I've linked to. – Eitan T Sep 15 '13 at 13:28
1

One line...

X = rand(); Y = rand();

... is that all you needed?

Gordon Bean
  • 4,272
  • 1
  • 32
  • 47