如何在Julia的某些数组的列中添加向量?

时间:2014-05-23 22:36:49

标签: dataframe julia

我知道,对于包DataFrames,可以通过简单的

来实现
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}上做同样的事情?

3 个答案:

答案 0 :(得分:4)

如果您知道最终数组中有多少行,则可以使用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

答案 1 :(得分:2)

循环遍历矩阵的行:

A = zeros(3,3)
for i = 1:3
  A[i,:] = [i, i+1, 2i]
end

答案 2 :(得分:2)

如果可能的话,最好从头开始创建具有所需列数的数组。这样,您只需填写这些列值即可。使用诸如hcat()之类的过程的解决方案将受到低效率的影响,因为它们每次都需要重新创建阵列。

如果您确实需要将列添加到现有数组中,那么如果您可以一次性添加所有列,而不是使用hcat()的循环,则会更好。例如。如果你从:

开始
n = 10; m = 5;
A = rand(n,m);

然后

A = [A rand(n, 3)]

比以下更快,内存效率更高:

for idx = 1:3
    A = hcat(A, rand(n))
end

E.g。比较这两者之间的速度和内存分配差异:

n = 10^5; m = 10;
A = rand(n,m);
n_newcol = 10;

function t1(A::Array, n_newcol)
    n = size(A,1)
    for idx = 1:n_newcol
        A = hcat(A, zeros(n))
    end
    return A
end

function t2(A::Array, n_newcol)
    n = size(A,1)
    [A zeros(n, n_newcol)]
end

# Stats after running each function once to compile
@time r1 = t1(A, n_newcol);  ## 0.145138 seconds (124 allocations: 125.888 MB, 70.58% gc time)
@time r2 = t2(A, n_newcol);  ## 0.011566 seconds (9 allocations: 22.889 MB, 39.08% gc time)