python - 为什么我使用 scipy.io.savemat 获取一行和一列而不是两列

标签 python matlab scipy

我得到两个数据,我想将其作为 .mat 文件 所以我使用了模块 scipy.io 和 scipy.io.savemat 但是当我打开文件时我得到这个: enter image description here

我想要两列为 950x1 而不是 1x950

这是代码:

import scipy.io as sio

x = Ramanshift1[64:1014]
Hem_I_Nor = pd.Normalization(y3_arpls)
sio.savemat(Hem_path+'/matdata.mat',{'ramanshift':x,'ramanspectra':Hem_I_Nor})

x 来自列 Hem_I_Nor 来自 pandas,所有这些都可以保存到 .csv 和两列

最佳答案

在 numpy session 中:

In [25]: from scipy.io import savemat
In [26]: savemat('test.mat', {'x':np.ones((3,),int), 'y': np.ones((3,1),int), 'z
    ...: ': np.ones((1,3),int)})

在 Octave session 中,加载后:

>> size(x),size(y),size(z)
ans =
   1   3

ans =
   3   1

ans =
   1   3

它是被制成 (1,n) 矩阵的一维数组:

x =
  1  1  1

对于二维数组,观察顺序。 MATLAB 使用 F。所以

In [31]: np.arange(12).reshape(3,4,order='F')
Out[31]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

另存为

>> w
w = 
   0   3   6   9
   1   4   7  10
   2   5   8  11

由于数组/矩阵顺序的固有差异,将值从一个传递到另一个可能会造成困惑。 savemat/loadmat 操作不一定有帮助。

查看文档,还有一个 oned_as 参数用于控制 (n,) 数组是否保存为 (n,1) 或 (1,n) .

关于python - 为什么我使用 scipy.io.savemat 获取一行和一列而不是两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53267043/

相关文章:

python - 在 Python 中用单个空格替换多个空格

python - 我该怎么做才能解决参数 'Cannot parse arguments with no order' 的错误?

algorithm - MATLAB:在矩阵中使用函数时避免循环

python - SciPy 中的并行优化

python - Python中有 "do ... until"吗?

Python 国际象棋 minimax 算法 - 如何玩黑色棋子(Bot 有白色棋子)

performance - 关于 Julia 和 Matlab 之间时序比较的奇怪观察

excel - MATLAB 中 xlsread 速度慢

Python SciPy n选k的可能情况

python - 在 numpy 中获取唯一行位置的更快方法是什么