【问题标题】:How to save Python 1D, 2D or 3D NumpPy array into MATLAB .mat如何将 Python 1D、2D 或 3D Numpy 数组保存到 MATLAB .mat
【发布时间】:2020-10-03 22:22:20
【问题描述】:

Python 的 SciPy 包有一个函数可以将 Python 变量保存到 MATLAB 的 .mat 文件中 https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html 但是,文档缺少示例,我不知道它想要作为输入的变量字典是什么。

假设我有一个 2D NumPy 数组 A 和一个 3D NumPy 数组 B,如何将它们保存到名为 my_arrays.mat.mat 文件中?

我可以在.mat 中保存多个变量,还是每个变量都需要一个文件?

如何将.mat 文件读入 Python 以及如何知道访问它们已加载的变量?

【问题讨论】:

    标签: python arrays matlab file-io scipy


    【解决方案1】:

    保存和加载示例

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多