How to add vectors to the columns of some array in Julia?

前端 未结 3 1979
悲哀的现实
悲哀的现实 2021-01-19 06:18

I know that, with package DataFrames, it is possible by doing simply

julia> df = DataFrame();

julia> for i in 1:3
          df[i] = [i, i         


        
3条回答
  •  花落未央
    2021-01-19 06:39

    If you know how many rows you have in your final Array, you can do it using hcat:

    # The number of lines of your final array
    numrows = 3
    
    # Create an empty array of the same type that you want, with 3 rows and 0 columns:
    a = Array(Int, numrows, 0)
    
    # Concatenate 3x1 arrays in your empty array:
    for i in 1:numrows
        b = [i, i+1, i*2] # Create the array you want to concatenate with a
        a = hcat(a, b)
    end
    

    Notice that, here you know that the arrays b have elements of the type Int. Therefore we can create the array a that have elements of the same type.

提交回复
热议问题