如何向Julia中的某些数组的列添加向量? [英] How to add vectors to the columns of some array in Julia?
本文介绍了如何向Julia中的某些数组的列添加向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道,使用包 DataFrames
可以通过简单地
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 |
...但是有空的 Array {Int64,2}
?
推荐答案
如果你知道你的最后一行有多少行数组,你可以使用 hcat
:
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
请注意,在这里,您知道数组 b
具有类型为 Int
的元素。因此,我们可以创建具有相同类型元素的数组 a
。
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.
这篇关于如何向Julia中的某些数组的列添加向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文