使用 h5py 将 matlab 加载到 python 后尺寸错误

问题描述 投票:0回答:1

我使用以下代码将 Matlab 加载到 Python 中

import h5py
import numpy as np
filepath = 'file.mat'
arrays = {}
f = h5py.File(filepath)
for k, v in f.items():
    arrays[k] = np.array(v)

a=arrays['data']

a.shape

问题是在Matlab中

>> size(dataset)

ans =

    64    50    21     2

在Python中

>>> a.shape
(2, 21, 50, 64)

知道为什么以及如何解决这个问题吗?

python matlab import hdf5 h5py
1个回答
0
投票

numpy.moveaxis()
可以做到这一点:

import numpy as np
tmp = np.zeros((2, 21, 50, 64))
tmp2 = np.moveaxis(tmp, [0, 1, 2, 3], [3, 2, 1, 0])

tmp2.shape
Out[5]: (64, 50, 21, 2)
© www.soinside.com 2019 - 2024. All rights reserved.