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+1, i*2]
end
julia> df
3x3 DataFrame
|-------|----|----|----|
| Row # | x1 | x2 | x3 |
| 1 | 1 | 2 | 3 |
| 2 | 2 | 3 | 4 |
| 3 | 2 | 4 | 6 |
... but are there any means to do the same on an empty Array{Int64,2}
?
Loop over the rows of the matrix:
If at all possible, it is best to create your Array with the desired number of columns from the start. That way, you can just fill in those column values. Solutions using procedures like
hcat()
will suffer from inefficiency, since they require re-creating the Array each time.If you do need to add columns to an already existing Array, you will be better off if you can add them all at once, rather than in a loop with
hcat()
. E.g. if you start with:then
will be faster and more memory efficient than:
E.g. compare the difference in speed and memory allocations between these two:
If you know how many rows you have in your final Array, you can do it using
hcat
:Notice that, here you know that the arrays
b
have elements of the typeInt
. Therefore we can create the arraya
that have elements of the same type.