matlab和numpy reshape 转换
问题描述:matlab的reshape转换是从列转换的,而numpy的reshape是从行转换的,故在将matlab代码转为python代码时会存在问题。如果使用reshape转换后维度虽然一致,元素不对。解决方式如下:pythonres = np.reshape(numpy矩阵, 维度, 维度, order='F')x = np.arange(0,24)y = np.reshape(x, (2,
·
问题描述:
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 =
列 1 至 9
0 1 2 3 4 5 6 7 8
列 10 至 18
9 10 11 12 13 14 15 16 17
列 19 至 24
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
更多推荐
已为社区贡献2条内容
所有评论(0)