Creating a 2x100 matrix with linspace

The following code produces a 2x100 matrix in MATLAB:

x0 = 0;
y0 = 0;
xf = 100;
yf = 200;
r0 = [x0; y0];
rf = [xf; yf];
r1 = [linspace(r0(1),rf(1),N); linspace(r0(2),rf(2),N)];

Trying the same thing in Julia using:

r1 = [linspace(r0[1],rf[1],N); linspace(r0[2],rf[2],N)];

And checking size(r1), I get an answer of (200,) which is a 1D array. How would I be able to replicate MATLAB’s results to produce a 2x100 matrix? Any help would be great, thanks very much.

[a; b] is vcat, which combine the two arrays in the first dimension, [a b] is hcat which combine the two arrays in the second dimension.
linspace returns a (vertical) vector so if you want to combine them in the other dimension you should use hcat so [linspace(r0[1],rf[1],N) linspace(r0[2],rf[2],N)] should work. (The result size will be (100, 2))

As Yichao already said, linspace returns a vector. If you want a 2x100 Array you can use

[linspace(r0[1],rf[1],N)'; linspace(r0[2],rf[2],N)']
1 Like

Thanks pabloferz, that did the trick

A side note: It is actually Matlab that is acting weird here. Matlab is supposed to be column-major, but still, functions like linspace and 1:10 return row vectors. Very odd, and frequently quite annoying.

3 Likes