MATLAB: When I generate a matrix with random numbers with the function rand, which is the order of generation

MATLABrandom number generator

I want to know if the matrix is generated vertically/columns or horizontally/rows.

Best Answer

Hi,
see this:
rng(25)
a = rand
rng(25)
b = rand(2,1)
rng(25)
x = rand(9,1)
rng(25)
y = rand(3,3)
which results in:
a =
0.8701
b =
0.8701
0.5823
x =
0.8701
0.5823
0.2788
0.1859
0.4111
0.1174
0.6850
0.4376
0.5562
y =
0.8701 0.1859 0.6850
0.5823 0.4111 0.4376
0.2788 0.1174 0.5562
So we know it is generated in columns - which also follows the linear indexing rules of matlab:
x =
0.8701
0.5823
0.2788
0.1859
0.4111
0.1174
0.6850
0.4376
0.5562
>> x1 = reshape(x,3,3)
x1 =
0.8701 0.1859 0.6850
0.5823 0.4111 0.4376
0.2788 0.1174 0.5562
>> y
y =
0.8701 0.1859 0.6850
0.5823 0.4111 0.4376
0.2788 0.1174 0.5562
Best regards
Stephan