Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill an array by row in Julia

Tags:

arrays

julia

I would like to fill an Array object by row in the Julia language. The reshape function wants to fill by column (Julia is column major).

julia> reshape(1:15, 3,5)
3x5 Array{Int64,2}:
 1  4  7  10  13
 2  5  8  11  14
 3  6  9  12  15

Is there a way to persuade it to fill by row? It feels like there should be an obvious answer, but I've not found one.

like image 440
conjectures Avatar asked Dec 15 '22 09:12

conjectures


2 Answers

One suggestion:

julia> reshape(1:15, 5, 3) |> transpose
3x5 Array{Int64,2}:
  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
like image 155
daycaster Avatar answered Dec 22 '22 23:12

daycaster


With array comprehension:

julia> [i+5*j for j=0:2,i=1:5]
3x5 Array{Int64,2}:
  1   2   3   4   5
  6   7   8   9  10
  11  12  13  14  15

Ah, it's just more than 10x times faster than other suggestion (actually, an embarrassing 100x on my initial benchmark).

like image 42
Dan Getz Avatar answered Dec 22 '22 23:12

Dan Getz