Multidimensional Array Comprehension in Julia

后端 未结 7 1411
既然无缘
既然无缘 2021-01-11 12:50

I\'m mucking about with Julia and can\'t seem to get multidimensional array comprehensions to work. I\'m using a nightly build of 0.20-pre for OSX; this could conceivably be

7条回答
  •  猫巷女王i
    2021-01-11 13:03

    Your intuition was to write [(x, y) for x in 1:5, y in 6:10], but what you need is to wrap the ranges in zip, like this:

    [i for i in zip(1:5, 6:10)]
    

    Which gives you something very close to what you need, namely:

    5-element Array{(Int64,Int64),1}:
    (1,6) 
    (2,7) 
    (3,8) 
    (4,9) 
    (5,10)
    

    To get exactly what you're looking for, you'll need:

    hcat([[i...] for i in zip(1:5, 6:10)]...)'
    

    This gives you:

    5x2 Array{Int64,2}:
    1   6
    2   7
    3   8
    4   9
    5  10
    

提交回复
热议问题