[英]How to save Python 1D, 2D or 3D NumpPy array into MATLAB .mat
Python's SciPy package has a function that saves Python variable into MATLAB's .mat
file https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html However, the documentation lacks examples, and I don't know what is the dictionary of variables it wants as an input. Python's SciPy package has a function that saves Python variable into MATLAB's .mat
file https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html However, the documentation lacks examples, and I don '不知道它想要作为输入的变量字典是什么。
Say I have a 2D NumPy array A
and a 3D NumPy array B
, how do I save them into a .mat
file called my_arrays.mat
?假设我有一个二维 NumPy 数组A
和一个 3D NumPy 数组B
,如何将它们保存到名为my_arrays.mat
的.mat
文件中。
Can I save multiple variables in .mat
or do I need a file to each variable?我可以在.mat
中保存多个变量,还是每个变量都需要一个文件?
How to read .mat
files into Python and how do I know to reach variables they have been loaded?如何将.mat
文件读入 Python 以及如何知道如何访问已加载的变量?
Examples for saving and loading保存和加载示例
import scipy.io as sio
# dummy data
A = np.random.randint(0,10,100)
B = np.random.randint(0,10,100)
# collect arrays in dictionary
savedict = {
'A' : A,
'B' : B,
}
# save to disk
sio.savemat('my_arrays.mat', savedict)
#load from disk
data = sio.loadmat('my_arrays.mat')
# alternative way to load from disk:
data = {}
sio.loadmat('my_arrays.mat', mdict=data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.