I'm trying to convert this code from matlab to python:
Matlab
lP =lP(:)/length(lP(:));
lM=lM(:)/length(lP(:));
My try:
lP=ravel(lP) / length(ravel(lP))
lM=ravel(lM) / length(ravel(lP))
Unfortunately, it seems that the flattening process ended in an incompatible way: Matlab ended with: 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
while python ended with 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]]
It seems as python made a different choice of the block flattening, does anyone see how can I reach the same results of matlab with python?
CodePudding user response:
The most likely issue is array ordering. Numpy uses row-major (C-style) ordering by default, while MATLAB only supports column-major (Fortran-style). So this should work:
lP=ravel(lP, 'F') / length(ravel(lP))
lM=ravel(lM, 'F') / length(ravel(lP))
However, numpy attaches a lot of stuff directly to the arrays, so this syntax is cleaner:
lP = lP.ravel('F') / length(lP.ravel())
lM = lM.ravel('F') / length(lP.ravel())
And the length of the flattened array is just its .size
, so it can be further simplified as:
lP = lP.ravel('F') / lP.size
lM = lM.ravel('F') / lP.size