You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用scipy.io生成n×1 cell数组格式.mat文件的问题咨询

Python导出n×1维度cell数组.mat文件的调整方法

你当前的代码生成的是一维numpy数组,scipy.io.savemat会默认将一维数组映射为MATLAB中的1×n行向量,因此得到的cell数组维度不符合要求。只需要将数组调整为二维n行1列结构即可。

修改后完整代码

import scipy.io as sio
import numpy as np

Label = ['A','Bob','C']
# reshape(-1,1)会自动计算行数,固定列数为1,将数组调整为n行1列结构
mat_file = {'label_mat': np.array(Label, dtype=object).reshape(-1, 1)}
sio.savemat(r'./test.mat', mat_file)

等价实现方式

你也可以用新增维度的语法实现同样效果:

mat_file = {'label_mat': np.array(Label, dtype=object)[:, np.newaxis]}

修改后导出的.mat文件在MATLAB中打开后,label_mat即为3×1的cell数组,符合要求。

内容的提问来源于stack exchange,提问作者Ricardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 12:15:04