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

发布于 2024-10-14 23:38:34 字数 1749 浏览 2 评论 0原文

由于某种原因,当我重新读取行向量时,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}。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-10-21 23:38:34

这是阅读的问题。来自帮助

[...] = hdf5read(..., 'V71Dimensions',
BOOL) 指定是否改变
大多数数据集读取自
文件。如果 BOOL 为 true,则 hdf5read
排列前两个维度
数据集,就像之前一样
版本(MATLAB 7.1 [R14SP3] 和
较早)。这种行为是有意为之
来解释方式上的差异
HDF5 和 MATLAB 表达数组
方面。 HDF5描述数据集
维度按行优先顺序排列; MATLAB
按列优先顺序存储数据。
然而,排列这些维度
可能无法正确反映意图
数据并可能无效
元数据。当 BOOL 为假时(
默认),数据维度
正确反映数据排序为
它被写在文件中——每个
输出变量的维度
匹配相同的尺寸
文件。

因此:

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

It's the reading that's an issue. From the 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.

Thus:

hdf5write('/tmp/data.h5','/data',rand(1,10));
size(hdf5read('/tmp/data.h5','/data','V71Dimensions',true))
ans =
     1    10
寄风 2024-10-21 23:38:34

我担心您将不得不使用 Matlab 的低级 HDF5 API。

在 Matlab 中,可以使用例如 H5.open(...)H5D.write(...) 等来使用低级 API。这些名称与 C 库的名称完全对应(请参阅 HDF5 文档 )。不过,它们采用的参数略有不同,但 matlab help 函数会告诉您需要了解的一切...

好消息是,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 matlab help 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文