从 python 创建 .mat 文件

发布于 2024-08-06 18:28:30 字数 423 浏览 4 评论 0原文

我有一个变量外显子 = [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10]]]。我想创建一个如下所示的 mat 文件

>>

exon : [3*2 double] [2*2 double]

当我使用 python 代码执行相同操作时,它显示错误消息。这是我的 python 代码,

import scipy.io
exon  = [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10]]]
scipy.io.savemat('/tmp/out.mat', mdict={'exon': (exon[0], exon[1])})

任何人都可以提出同样的建议,那就太好了。 提前致谢 维品TS

I have a variable exon = [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10]]]. I would like to create a mat file like the following

>>

exon : [3*2 double] [2*2 double]

When I used the python code to do the same it is showing error message. here is my python code

import scipy.io
exon  = [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10]]]
scipy.io.savemat('/tmp/out.mat', mdict={'exon': (exon[0], exon[1])})

It will be great anyone can give a suggestion for the same.
thanks in advance
Vipin T S

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2024-08-13 18:28:30

您似乎希望在 Matlab 中将两个不同的数组链接到相同的变量名。那是不可能的。在 MATLAB 中,您可以拥有包含其他数组的元胞数组或结构体,但不能仅将一组数组分配给单个变量(这就是 mdict={'exon': (exon[0], exon1)) - 中没有元组的概念MATLAB。

您还需要将对象设为 numpy 数组:

import numpy as np
exon = [ np.array([[1, 2], [3, 4], [5, 6]]), np.array([[7, 8], [9, 10]]) ]

这里有 scipy 文档 这里< /a> 包含如何保存不同 Matlab 类型的详细信息,但假设您需要元胞数组:

obj_arr = np.zeros((2,), dtype=np.object)
obj_arr[0] = exon[0]
obj_arr[1] = exon[1]
scipy.io.savemat('/tmp/out.mat', mdict={'exon': obj_arr})

这将在 matlab 中产生以下结果:

“matlab

或可能(未经测试):

obj_arr = np.array(exon, dtype=np.object)

You seem to want two different arrays linked to same variable name in Matlab. That is not possible. In MATLAB you can have cell arrays, or structs, which contain other arrays, but you cannot have just a tuple of arrays assigned to a single variable (which is what you have in mdict={'exon': (exon[0], exon1)) - there is no concept of a tuple in Matlab.

You will also need to make your objects numpy arrays:

import numpy as np
exon = [ np.array([[1, 2], [3, 4], [5, 6]]), np.array([[7, 8], [9, 10]]) ]

There is scipy documentation here with details of how to save different Matlab types, but assuming you want cell array:

obj_arr = np.zeros((2,), dtype=np.object)
obj_arr[0] = exon[0]
obj_arr[1] = exon[1]
scipy.io.savemat('/tmp/out.mat', mdict={'exon': obj_arr})

this will result in the following at matlab:

code result in matlab

or possibly (untested):

obj_arr = np.array(exon, dtype=np.object)
天煞孤星 2024-08-13 18:28:30

Sage 是一款开源数学软件,旨在将 python 语法和 python 解释器与 Matlab、Octave、Mathematica 等其他工具捆绑在一起......

也许您想看看它:

Sage is an open source mathematics software which aims at bundling together the python syntax and the python interpreter with other tools like Matlab, Octave, Mathematica, etc...

Maybe you want to have a look at it:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文