Multidimensional Array Comprehension in Julia

后端 未结 7 1266
既然无缘
既然无缘 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条回答
  •  被撕碎了的回忆
    2021-01-11 13:01

    I think you are just reading the list comprehension wrong

    julia> [x+5y for  x in 1:5, y in 0:1]
    5x2 Array{Int64,2}:
     1   6
     2   7
     3   8
     4   9
     5  10
    

    When you use them in multiple dimensions you get two variables and need a function for the cell values based on the coordinates

    For your second question I think that you should reconsider your requirements. Julia uses typed arrays for performance and storing different types in different columns is possible. To get an untyped array you can use {} instead of [], but I think the better solution is to have an array of tuples (Int, Bool) or even better just use two arrays (one for the ints and one for the bool).

    julia> [(i,false) for i in 1:5]
    5-element Array{(Int64,Bool),1}:
     (1,false)
     (2,false)
     (3,false)
     (4,false)
     (5,false)
    

提交回复
热议问题