问题描述:
matlab的reshape转换是从列转换的,而numpy的reshape是从行转换的,故在将matlab代码转为python代码时会存在问题。
如果使用reshape转换后维度虽然一致,元素不对。

解决方式如下:

python

res = np.reshape(numpy矩阵, 维度, 维度, order='F')
x = np.arange(0,24)
y = np.reshape(x, (2,3,4), order='F')
print(y.shape)
print(y)
(2, 3, 4)
[[[ 0  6 12 18]
  [ 2  8 14 20]
  [ 4 10 16 22]]

 [[ 1  7 13 19]
  [ 3  9 15 21]
  [ 5 11 17 23]]]

matlab

x = [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

x =19

     0     1     2     3     4     5     6     7     81018

     9    10    11    12    13    14    15    16    171924

    18    19    20    21    22    23

reshape(x,2,3,4)
ans(:,:,1) =

     0     2     4
     1     3     5


ans(:,:,2) =

     6     8    10
     7     9    11


ans(:,:,3) =

    12    14    16
    13    15    17


ans(:,:,4) =

    18    20    22
    19    21    23
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐