python 等价于 Matlab 展平
python equivalence to Matlab flattening
我正在尝试将此代码从 matlab 转换为 python:
Matlab
lP =lP(:)/length(lP(:));
lM=lM(:)/length(lP(:));
我的尝试:
lP=ravel(lP) / length(ravel(lP))
lM=ravel(lM) / length(ravel(lP))
不幸的是,扁平化过程似乎以不兼容的方式结束:
Matlab 结束于:
lP =
0
0
282.158675652645
245.206579787908
180.857334994335
145.295985608783
98.6111323759356
78.6541285064253
0
0
235.988440426672
252.705377114354
149.700677619887
149.441877249856
94.6327116403152
84.5507642298126
201.33034579544
168.5504445176
112.020019512079
100.029337003081
81.9696997469859
77.2791884039185
53.8857894853351
47.1052112310254
201.005392514542
170.334647413678
102.794197425736
117.624998880039
63.5474961850914
85.6855606877716
40.2032122529948
55.9023649432995
113.662165613052
105.200545764806
66.249378944203
60.9266562306452
46.1411501598512
44.4884016760164
31.2403038422145
31.8768645697115
96.6943687173171
94.3667944720464
63.9090790984522
63.4837480098802
43.801313157197
47.5765809365761
27.4423759004399
31.6859217744796
74.7724121990454
66.780700287442
35.6152335123858
37.2132749966295
27.2233609912034
28.2363756837094
18.7629642336031
19.217036195859
55.1857268319806
49.2057110093965
35.3827383680906
33.9961168439708
26.0418645945658
26.2135571149649
17.8586181284183
18.6567862728906
而 python 以 lP=:
结尾
[[ 0. ],
[ 0. ],
[282.15867565],
[245.20657979],
[ 35.61523351],
[ 37.213275 ],
[ 74.7724122 ],
[ 66.78070029],
[ 0. ],
[ 0. ],
[235.98844043],
[252.70537711],
[ 35.38273837],
[ 33.99611684],
[ 55.18572683],
[ 49.20571101],
[201.3303458 ],
[168.55044452],
[112.02001951],
[100.029337 ],
[ 66.24937894],
[ 60.92665623],
[113.66216561],
[105.20054576],
[201.00539251],
[170.33464741],
[102.79419743],
[117.62499888],
[ 63.9090791 ],
[ 63.48374801],
[ 96.69436872],
[ 94.36679447],
[ 53.88578949],
[ 47.10521123],
[ 81.96969975],
[ 77.2791884 ],
[ 46.14115016],
[ 44.48840168],
[ 31.24030384],
[ 31.87686457],
[ 40.20321225],
[ 55.90236494],
[ 63.54749619],
[ 85.68556069],
[ 43.80131316],
[ 47.57658094],
[ 27.4423759 ],
[ 31.68592177],
[ 98.61113238],
[ 78.65412851],
[180.85733499],
[145.29598561],
[ 27.22336099],
[ 28.23637568],
[ 18.76296423],
[ 19.2170362 ],
[ 94.63271164],
[ 84.55076423],
[149.70067762],
[149.44187725],
[ 26.04186459],
[ 26.21355711],
[ 17.85861813],
[ 18.65678627]]
似乎 python 对块展平做了不同的选择,有谁知道我怎样才能用 python 达到与 matlab 相同的结果?
最有可能的问题是数组排序。 Numpy 默认使用行优先(C 风格)排序,而 MATLAB 仅支持列优先(Fortran 风格)。所以这应该有效:
lP=ravel(lP, 'F') / length(ravel(lP))
lM=ravel(lM, 'F') / length(ravel(lP))
然而,numpy 将很多东西直接附加到数组,所以这个语法更清晰:
lP = lP.ravel('F') / length(lP.ravel())
lM = lM.ravel('F') / length(lP.ravel())
而展平数组的长度正好是.size
,所以可以进一步简化为:
lP = lP.ravel('F') / lP.size
lM = lM.ravel('F') / lP.size
我正在尝试将此代码从 matlab 转换为 python:
Matlab
lP =lP(:)/length(lP(:));
lM=lM(:)/length(lP(:));
我的尝试:
lP=ravel(lP) / length(ravel(lP))
lM=ravel(lM) / length(ravel(lP))
不幸的是,扁平化过程似乎以不兼容的方式结束: Matlab 结束于: lP =
0
0
282.158675652645
245.206579787908
180.857334994335
145.295985608783
98.6111323759356
78.6541285064253
0
0
235.988440426672
252.705377114354
149.700677619887
149.441877249856
94.6327116403152
84.5507642298126
201.33034579544
168.5504445176
112.020019512079
100.029337003081
81.9696997469859
77.2791884039185
53.8857894853351
47.1052112310254
201.005392514542
170.334647413678
102.794197425736
117.624998880039
63.5474961850914
85.6855606877716
40.2032122529948
55.9023649432995
113.662165613052
105.200545764806
66.249378944203
60.9266562306452
46.1411501598512
44.4884016760164
31.2403038422145
31.8768645697115
96.6943687173171
94.3667944720464
63.9090790984522
63.4837480098802
43.801313157197
47.5765809365761
27.4423759004399
31.6859217744796
74.7724121990454
66.780700287442
35.6152335123858
37.2132749966295
27.2233609912034
28.2363756837094
18.7629642336031
19.217036195859
55.1857268319806
49.2057110093965
35.3827383680906
33.9961168439708
26.0418645945658
26.2135571149649
17.8586181284183
18.6567862728906
而 python 以 lP=:
结尾 [[ 0. ],
[ 0. ],
[282.15867565],
[245.20657979],
[ 35.61523351],
[ 37.213275 ],
[ 74.7724122 ],
[ 66.78070029],
[ 0. ],
[ 0. ],
[235.98844043],
[252.70537711],
[ 35.38273837],
[ 33.99611684],
[ 55.18572683],
[ 49.20571101],
[201.3303458 ],
[168.55044452],
[112.02001951],
[100.029337 ],
[ 66.24937894],
[ 60.92665623],
[113.66216561],
[105.20054576],
[201.00539251],
[170.33464741],
[102.79419743],
[117.62499888],
[ 63.9090791 ],
[ 63.48374801],
[ 96.69436872],
[ 94.36679447],
[ 53.88578949],
[ 47.10521123],
[ 81.96969975],
[ 77.2791884 ],
[ 46.14115016],
[ 44.48840168],
[ 31.24030384],
[ 31.87686457],
[ 40.20321225],
[ 55.90236494],
[ 63.54749619],
[ 85.68556069],
[ 43.80131316],
[ 47.57658094],
[ 27.4423759 ],
[ 31.68592177],
[ 98.61113238],
[ 78.65412851],
[180.85733499],
[145.29598561],
[ 27.22336099],
[ 28.23637568],
[ 18.76296423],
[ 19.2170362 ],
[ 94.63271164],
[ 84.55076423],
[149.70067762],
[149.44187725],
[ 26.04186459],
[ 26.21355711],
[ 17.85861813],
[ 18.65678627]]
似乎 python 对块展平做了不同的选择,有谁知道我怎样才能用 python 达到与 matlab 相同的结果?
最有可能的问题是数组排序。 Numpy 默认使用行优先(C 风格)排序,而 MATLAB 仅支持列优先(Fortran 风格)。所以这应该有效:
lP=ravel(lP, 'F') / length(ravel(lP))
lM=ravel(lM, 'F') / length(ravel(lP))
然而,numpy 将很多东西直接附加到数组,所以这个语法更清晰:
lP = lP.ravel('F') / length(lP.ravel())
lM = lM.ravel('F') / length(lP.ravel())
而展平数组的长度正好是.size
,所以可以进一步简化为:
lP = lP.ravel('F') / lP.size
lM = lM.ravel('F') / lP.size