matlab - 如何在 MATLAB 中将行向量保存为 HDF?

标签 matlab hdf5

出于某种原因,当我重新读取行向量时,MATLAB 中的 hdf5write 方法会自动将它们转换为列向量:

>> hdf5write('/tmp/data.h5','/data',rand(1,10));
>> size(hdf5read('/tmp/data.h5','/data'))

ans =

    10     1

但是,对于第三维的行向量,它返回得很好:

>> hdf5write('/tmp/data.h5','/data',rand(1,1,10));
>> size(hdf5read('/tmp/data.h5','/data'))

ans =

     1     1    10

如何让 hdf5write 为行向量做正确的事情?它们应该以 1 x 10 的形式返回,而不是 10 x 1。

编辑问题稍微复杂一些,因为我稍后使用基于c的mex而不是hdf5read来实际读取数据。此外,问题确实出现在hdf5write中,这在hdf5文件本身中可见:

>> hdf5write('/tmp/data.h5','/data',randn(1,10));
>> ! h5ls /tmp/data.h5

data                     Dataset {10}

即数据以一维数组的形式保存在hdf5文件中。为了进行比较,我尝试使用实际的二维矩阵(以显示它的外观)、一维列向量、沿第三维的一维向量进行相同的操作,并且,如果想尝试一下,请尝试 V71Dimensions 技巧,位于 hdf5readhdf5write 的帮助中:

>> hdf5write('/tmp/data.h5','/data',randn(10,1)); %1-d col vector
>> ! h5ls /tmp/data.h5

data                     Dataset {10}
>> hdf5write('/tmp/data.h5','/data',randn(1,1,10)); %1-d vector along 3rd dim; annoying
>> ! h5ls /tmp/data.h5

data                     Dataset {10, 1, 1}
>> hdf5write('/tmp/data.h5','/data',randn(2,5)); %2-d matrix. notice the reversal in dim order
>> ! h5ls /tmp/data.h5

data                     Dataset {5, 2}
>> hdf5write('/tmp/data.h5','/data',randn(1,10),'V71Dimensions',true); %1-d row; option does not help
>> ! h5ls /tmp/data.h5

data                     Dataset {10}

所以,问题似乎确实出在 hdf5write 中。 'V71Dimensions' 标志没有帮助:生成的 hdf5 文件仍然是数据集 {10},而不是数据集 {10,1}。

最佳答案

这是阅读的问题。来自 help

[...] = hdf5read(..., 'V71Dimensions', BOOL) specifies whether to change the majority of data sets read from the file. If BOOL is true, hdf5read permutes the first two dimensions of the data set, as it did in previous releases (MATLAB 7.1 [R14SP3] and earlier). This behavior was intended to account for the difference in how HDF5 and MATLAB express array dimensions. HDF5 describes data set dimensions in row-major order; MATLAB stores data in column-major order. However, permuting these dimensions may not correctly reflect the intent of the data and may invalidate metadata. When BOOL is false (the default), the data dimensions correctly reflect the data ordering as it is written in the file — each dimension in the output variable matches the same dimension in the file.

因此:

hdf5write('/tmp/data.h5','/data',rand(1,10));
size(hdf5read('/tmp/data.h5','/data','V71Dimensions',true))
ans =
     1    10

关于matlab - 如何在 MATLAB 中将行向量保存为 HDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4823844/

相关文章:

python - 没有 dtype ('<U1' 的转换路径)问题

python - 使用 h5py 删除 hdf5 数据集

hdf5 - 将几个 hdf5 文件合并到一个 pytable 中

python - 以 SQLite 和 HDF5 格式从 numpy、scipy 导出/导入

matlab - 在 "hold on"后面加上 "figure"导致剧情不一样

matlab - 从数组中排除元素

Matlab : How to represent a real number as binary

matlab - 调用匿名函数,输入变量之间不使用逗号

c++ - 在matlab中移动文件

python - 在 Python 中加载 csv 并保存 HDF5