从 Python 到 MATLAB 的矩阵 [英] Matrix from Python to MATLAB
问题描述
我现在正在使用 Python 和 MATLAB,我在 Python 中有一个 2D 数组,我需要将其写入文件,然后才能将其作为矩阵读入 MATLAB.关于如何做到这一点的任何想法?
I'm working with Python and MATLAB right now and I have a 2D array in Python that I need to write to a file and then be able to read it into MATLAB as a matrix. Any ideas on how to do this?
谢谢!
推荐答案
如果你使用numpy/scipy,你可以使用scipy.io.savemat
函数:
If you use numpy/scipy, you can use the scipy.io.savemat
function:
import numpy, scipy.io
arr = numpy.arange(9) # 1d array of 9 numbers
arr = arr.reshape((3, 3)) # 2d array of 3x3
scipy.io.savemat('c:/tmp/arrdata.mat', mdict={'arr': arr})
现在,您可以使用 File -> Load Data 将此数据加载到 MATLAB 中.选择文件,arr
变量(一个 3x3 矩阵)将在您的环境中可用.
Now, you can load this data into MATLAB using File -> Load Data. Select the file and the arr
variable (a 3x3 matrix) will be available in your environment.
注意:我是在 scipy 0.7.0 上做的.(scipy 0.6 在 scipy.io.mio
模块中有 savemat
.)参见 更多详细信息的最新文档
Note: I did this on scipy 0.7.0. (scipy 0.6 has savemat
in the scipy.io.mio
module.) See the latest documentation for more detail
更新链接感谢 @gnovice.
这篇关于从 Python 到 MATLAB 的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!