在python中创建v7.3的.mat文件 [英] Creating a .mat file of v7.3 in python

查看:474
本文介绍了在python中创建v7.3的.mat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在python或matlab中执行涉及60000X70000矩阵的乘法.我有一个16GB的RAM,能够轻松加载矩阵的每一行(这是我所需要的).我可以在python中整体创建矩阵,但不能在matlab中创建矩阵. 无论如何,我可以使用h5py或scipy将数组另存为v7.3的.mat文件,以便分别加载每一行吗?

I need to perform multiplication involving 60000X70000 matrix either in python or matlab. I have a 16GB RAM and am able to load each row of the matrix easily (which is what I require). I am able to create the matrix as a whole in python but not in matlab. Is there anyway I can save the array as .mat file of v7.3 using h5py or scipy so that I can load each row separately?

推荐答案

对于MATLAB v7.3,您可以使用需要h5pyhdf5storage,在此处下载文件,解压缩,然后从命令中键入:python setup.py install迅速的. https://pypi.python.org/pypi/hdf5storage

For MATLAB v7.3 you can use hdf5storage which requires h5py, download the file here, extract, then type: python setup.py install from a command prompt. https://pypi.python.org/pypi/hdf5storage

import h5py
import hdf5storage
import numpy as np

matfiledata = {} # make a dictionary to store the MAT data in
matfiledata[u'variable1'] = np.zeros(100) # *** u prefix for variable name = unicode format, no issues thru Python 3.5; advise keeping u prefix indicator format based on feedback despite docs ***
matfiledata[u'variable2'] = np.ones(300)
hdf5storage.write(matfiledata, '.', 'example.mat', matlab_compatible=True)

如果MATLAB无法一次加载全部内容,我认为您必须将其保存在不同的变量matfiledata[u'chunk1'] matfiledata[u'chunk2'] matfiledata[u'chunk3']等中.

If MATLAB can't load the whole thing at once, I think you'll have to save it in different variables matfiledata[u'chunk1'] matfiledata[u'chunk2'] matfiledata[u'chunk3'] etc.

然后在MATLAB中,如果将每个块另存为变量

Then in MATLAB if you save each chunk as a variable

load(filename,'chunk1')
do stuff...
clear chunk1
load(filename,'chunk2')
do stuff...
clear chunk2

hdf5storage.savemat具有一个参数,该参数允许将来将文件正确读取到Python中,因此值得签出,并遵循scipy.io.loadmat格式...尽管您可以在保存数据时执行类似的操作从MATLAB轻松导入回Python:

The hdf5storage.savemat has a parameter to allow the file to be read into Python correctly in the future so worth checking out, and follows the scipy.io.loadmat format... although you can do something like this if saving data from MATLAB to make it easy to import back into Python:

MATLAB    
save('example.mat','-v7.3')
Python
matdata = hdf5storage.loadmat('example.mat')

这将作为字典重新加载到Python中,然后您可以将其转换为所需的任何数据类型.

That will load back into Python as a dictionary which you can then convert into whatever datatypes you need.

这篇关于在python中创建v7.3的.mat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆