交叉发布here ,但是如何使用 HDF5 在 Julia 中保存数组数组?
在我的特定情况下,我有一个包含 10,000 个不同长度的数组的数组。我希望 10,000 个数组成为“组”的一部分,但为每个数组创建新的数据集/组会使读取文件变得非常慢,因此我正在寻找替代方案。
最佳答案
您可以将数组数组展平为一个数组,其中一列包含原始数据,另一列表示该数据最初来自第 i 个数组。
using HDF5
# Define your array of arrays.
arr = [[1,2],[3,4,5]]
# Open your hdf5 file
h5open("data.hdf5", "w") do f
# Create a dataset with the length of all your arrays combined.
N = sum(length.(arr))
d_create(f, "X", Int, ((2,N),(2,-1)), "chunk", (1,1000))
n = 1
for i in 1:length(arr)
m = length(arr[i])
f["X"][1, n:n+m-1] = fill(i, m)
f["X"][2, n:n+m-1] = arr[i]
n+=m
end
print(f["X"][:,:])
end
然后数组存储如下:
> [1 1 2 2 2; 1 2 3 4 5]
关于julia - 保存数组的数组、HDF5、Julia,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58983994/