如何在 MATLAB 中将行向量保存为 HDF?
由于某种原因,当我重新读取行向量时,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
技巧,位于 hdf5read
和 hdf5write
的帮助中:
>> 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}。
For some reason, the hdf5write
method in MATLAB is automatically converting my row vectors to column vectors when I re-read them:
>> hdf5write('/tmp/data.h5','/data',rand(1,10));
>> size(hdf5read('/tmp/data.h5','/data'))
ans =
10 1
However, for a row vector in the third dimension, it comes back just fine:
>> hdf5write('/tmp/data.h5','/data',rand(1,1,10));
>> size(hdf5read('/tmp/data.h5','/data'))
ans =
1 1 10
How can I get hdf5write
to do the right thing for row vectors? They should be coming back as 1 x 10, not 10 x 1.
edit the problem is slightly more complicated because I am using c-based mex to actually read the data later, instead of hdf5read
. Moreover, the problem really is in hdf5write
, and this is visible in the hdf5 files themselves:
>> hdf5write('/tmp/data.h5','/data',randn(1,10));
>> ! h5ls /tmp/data.h5
data Dataset {10}
that is, the data is saved as a 1-dimensional array in the hdf5 file. For comparison, I try the same thing with an actual 2-d matrix (to show what it looks like), a 1-d column vector, a 1-d vector along the third dimension, and, for kicks, try the V71Dimensions
trick which is in the help for both hdf5read
and hdf5write
:
>> 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}
So, the problem does seem to be in hdf5write
. The 'V71Dimensions'
flag does not help: the resultant hdf5 file is still a Dataset {10} instead of a Dataset {10,1}.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是阅读的问题。来自帮助
因此:
It's the reading that's an issue. From the help
Thus:
我担心您将不得不使用 Matlab 的低级 HDF5 API。
在 Matlab 中,可以使用例如
H5.open(...)
、H5D.write(...)
等来使用低级 API。这些名称与 C 库的名称完全对应(请参阅 HDF5 文档 )。不过,它们采用的参数略有不同,但 matlabhelp
函数会告诉您需要了解的一切...好消息是,Matlab 版本的 API 仍然不那么冗长比C版本。例如,您不必手动关闭数据类型、数据空间等,因为当变量超出范围时,Matlab 会为您关闭它们。
I'm affraid for this you will have to use the low-level HDF5 API of Matlab.
In Matlab, the low-level API is available using for instance
H5.open(...)
,H5D.write(...)
and so on. The names correspond exactly to those of the C library (see the HDF5 doc). However there is a slight difference in the arguments they take, but the matlabhelp
function will tell you everything you need to know...The good news is that the Matlab version of the API is still less verbose than the C version. For instance, you don't have to close manually the datatypes, dataspaces, etc. since Matlab closes them for you when the variables go out of scope.