Reshape 读取和摆放顺序: Matlab VS Python

sq-feng / 2025-02-18 / 原文

  1. Matlab读取:列优先(高维矩阵:靠前的维度优先)
>> b = [1,2;3,4];
>> reshape(b,1,4)

ans =

     1     3     2     4
  1. Matalb填充:列优先(高维矩阵:靠前的维度优先)
>> b = [1      2      3      4]

b =

     1     2     3     4

>> reshape(b,2,2)

ans =

     1     3
     2     4
  1. Python读取:行优先(高维矩阵:靠后的维度优先)
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.reshape(a,(1,4))
print(b)

[[1 2 3 4]]
  1. Python填充:行优先(高维矩阵:靠后的维度优先)
import numpy as np
a = np.array([1,2,3,4])
b = np.reshape(a,(2,2))
print(b)

[[1 2]
 [3 4]]

这都是默认情况,Python通过传参'C' or 'F'还是能改变顺序的。
值得一提的是,对于Matlab,没有传参改变填充顺序的机会,只能通过矩阵转置来处理。