我在MATLAB中有以下代码,我想把它翻译成python。
colB = [105.0767
105.5614
106.0449
106.5258
107.0050
107.4866
107.9851
108.4947
109.0013
109.4960
109.9605
110.4045
110.8459
111.2929
111.7416
112.1844
112.6098
113.0130
113.4014
113.7805
114.1556
114.5232
114.8870
115.2418
115.5799
115.8978
116.1978
116.4827
116.7571
117.0246
117.2874
117.5453
117.7982
118.0483
118.2948
118.5366
118.7699
118.9948
119.2122
119.4192
119.6140
119.7980
119.9737
120.1432
120.3069
120.4619
120.6076
120.7430
120.8684
120.9849
121.0936
121.1953
121.2909
121.3822
121.4687
121.5482
121.6184
121.6793
121.7327
121.7801
121.8223
121.8599
121.8927
121.9187
121.9363
121.9468
121.9504
121.9476
121.9385
121.9211
121.8975
121.8681
121.8334
121.7908
121.7433
121.6928
121.6422
121.5917
121.5411
121.4906
121.4401
121.3895
121.3390
121.2884
121.2379
121.1873
121.1368
121.0862
121.0357
120.9851
120.9346
120.8840
120.8335
120.7829
120.7324
120.6818
120.6313
120.5807
120.5302
120.4796
120.4291
120.3785
120.3280
120.2774
120.2269
120.1764
120.1258
120.0753
120.0247
119.9742
119.9236
119.8731
119.8225
119.7720
119.7214
119.6709
119.6203
119.5698
119.5192
119.4687
119.4181
119.3676
119.3170
119.2665
119.2159
119.1654
119.1148
119.0643
119.0138
118.9632
118.9127
118.8621
118.8116
118.7610
118.7105
118.6599
118.6094
118.5588
118.5083
118.4577
118.4072
118.3566
118.3061
118.2555
118.2050
118.1544
118.1039
118.0533
118.0028
117.9522
117.9017
117.8512
117.8006
117.7501
117.6995
117.6490
117.5984
117.5479
117.4973
117.4468
117.3962
117.3457
117.2951
117.2446
117.1940
117.1435
117.0929
117.0424
116.9918
116.9413
116.8907
116.8402
116.7896
116.7391
116.6885
116.6380
116.5875
116.5369
116.4864
116.4358
116.3853
116.3347
116.2842
116.2336
116.1831
116.1325
116.0820
116.0314
115.9809
115.9303
115.8798
115.8292
115.7787
115.7281
115.6776
115.6270
115.5765
115.5259
115.4754
115.4249
115.3743
115.3238
115.2732
115.2227
115.1721
115.1216
115.0710
115.0205
114.9699
114.9194
114.8688
114.8183
114.7677
114.7172
114.6666
114.6092
114.5455
114.4754
114.3984
114.3143
114.2233
114.1255
114.0206
113.9087
113.7891
113.6609
113.5247
113.3807
113.2295
113.0714
112.9060
112.7329
112.5516
112.3614
112.1599
111.9472
111.7239
111.4946
111.2590
111.0142
110.7592
110.4902
110.2065
109.9060
109.5896
109.2629
108.9231
108.5721
108.2080
107.8307
107.4411
107.0378
106.6259
106.2053
105.7731
105.3310
104.8843
104.4318
103.9728
103.5068
103.0305
102.5438
102.0497
101.5431
101.0189
100.4735
99.9116
99.3448
98.7798
98.2161
97.6443
97.0568
96.4461
95.8170
95.1775
94.5390
93.9049
93.2778
92.6581
92.0322
91.3820
90.6952
89.9731
89.2305
88.4928
87.7724
87.0845
86.4269
85.7820
85.1406
84.5003
83.8508
83.1875
82.5056
81.8090
81.1094
80.4259
79.7570
79.1035
78.4643]
CD4_B0_col = zeros(length(B0_col),1);
for i = 1:length(B0_col)
if (i==1)
CD4_B0_col(i) = abs(B0_col(i+1)-B0_col(i));
elseif (i==2 || i==length(B0_col)-1)
CD4_B0_col(i) = abs(B0_col(i+1)-2*B0_col(i)+B0_col(i-1));
elseif (i>2 && i<length(B0_col)-1)
CD4_B0_col(i) = abs(B0_col(i+2)-4*B0_col(i+1)+6*B0_col(i)-4*B0_col(i-1)+B0_col(i-2));
else
CD4_B0_col(i) = abs(B0_col(i-1)-B0_col(i));
end
end
我的python代码如下(我认为很好):
B0_col = np.array( [105.07667879044652, 105.5613777248652, 106.0449081509817, 106.52575610637302, 107.00499564553266, 107.48657205611589, 107.98511787367873, 108.49471095184933, 109.00129898214784, 109.49604219824592, 109.96049386457014, 110.40445303428619, 110.84592332323919, 111.29290125795949, 111.7415584650944, 112.18438066218506, 112.60982712605063, 113.01303084391708, 113.40138952129287, 113.7805363592266, 114.15560862629165, 114.52317260339136, 114.88701102708629, 115.24183634416714, 115.57988452380293, 115.89781586362314, 116.19784645709673, 116.4826675292113, 116.75709650065224, 117.02457857577812, 117.28744117483882, 117.54527523058013, 117.79822007489527, 118.04831649902856, 118.29478177778603, 118.53663147120618, 118.76985288681152, 118.99479947173607, 119.21222557380734, 119.41916861018635, 119.61404721770822, 119.79804992737525, 119.97372452112555, 120.14323005585807, 120.30689756912741, 120.46191225162659, 120.6075943070223, 120.74303031591633, 120.86840350845637, 120.98492862517824, 121.09364011563227, 121.1952892913203, 121.29085444658267, 121.38224884511483, 121.46871165942557, 121.54820239726212, 121.61836875124048, 121.67925196821798, 121.73267534516354, 121.78012383069321, 121.82234066107127, 121.85987220519327, 121.89268620284493, 121.91870073695361, 121.93629473570756, 121.9467770217112, 121.9504011345692, 121.9475647113194, 121.93851926831042, 121.92112418028479, 121.89749757959407, 121.86807064574144, 121.8333735344001, 121.79075613184469, 121.7433387027579, 121.69279080391163, 121.64224290506519, 121.5916950062189, 121.54114710737258, 121.4905992085263, 121.44005130967984, 121.38950341083357, 121.33895551198727, 121.288407613141, 121.23785971429452, 121.18731181544824, 121.13676391660195, 121.08621601775567, 121.03566811890923, 120.98512022006291, 120.93457232121662, 120.88402442237035, 120.83347652352396, 120.7829286246776, 120.73238072583132, 120.68183282698502, 120.6312849281387, 120.58073702929227, 120.53018913044599, 120.4796412315997, 120.42909333275342, 120.37854543390694, 120.32799753506069, 120.2774496362144, 120.2269017373681, 120.1763538385216, 120.12580593967536, 120.07525804082904, 120.02471014198275, 119.9741622431363, 119.92361434429003, 119.87306644544371, 119.82251854659745, 119.771970647751, 119.72142274890471, 119.6708748500584, 119.62032695121212, 119.56977905236577, 119.51923115351939, 119.46868325467308, 119.41813535582679, 119.3675874569805, 119.31703955813406, 119.26649165928778, 119.21594376044146, 119.1653958615952, 119.11484796274873, 119.06430006390246, 119.01375216505616, 118.96320426620987, 118.9126563673634, 118.86210846851714, 118.81156056967082, 118.76101267082454, 118.7104647719781, 118.6599168731318, 118.60936897428549, 118.55882107543921, 118.50827317659282, 118.4577252777465, 118.40717737890019, 118.35662948005391, 118.3060815812076, 118.25553368236118, 118.20498578351489, 118.15443788466858, 118.10388998582226, 118.05334208697583, 118.00279418812956, 117.95224628928327, 117.90169839043696, 117.85115049159053, 117.80060259274423, 117.75005469389791, 117.69950679505163, 117.64895889620517, 117.5984109973589, 117.54786309851261, 117.49731519966632, 117.44676730081991, 117.3962194019736, 117.34567150312728, 117.29512360428102, 117.24457570543467, 117.19402780658828, 117.14347990774198, 117.09293200889569, 117.04238411004938, 116.99183621120295, 116.94128831235665, 116.89074041351033, 116.84019251466407, 116.78964461581765, 116.73909671697135, 116.68854881812506, 116.63800091927874, 116.58745302043232, 116.53690512158605, 116.4863572227397, 116.43580932389344, 116.38526142504699, 116.3347135262007, 116.2841656273544, 116.23361772850811, 116.18306982966172, 116.13252193081537, 116.08197403196908, 116.03142613312278, 115.98087823427643, 115.93033033543004, 115.87978243658375, 115.82923453773745, 115.77868663889116, 115.72813874004474, 115.67759084119845, 115.62704294235215, 115.57649504350583, 115.52594714465944, 115.47539924581312, 115.4248513469668, 115.3743034481205, 115.32375554927411, 115.27320765042779, 115.22265975158147, 115.17211185273518, 115.12156395388878, 115.07101605504246, 115.02046815619617, 114.96992025734988, 114.91937235850351, 114.86882445965715, 114.81827656081084, 114.76772866196455, 114.71718076311825, 114.66663286427186, 114.60915104893375, 114.5455026712127, 114.47536495399498, 114.39841494847923, 114.31431176781162, 114.22331628896791, 114.12550643945637, 114.02058169962378, 113.90872568640812, 113.78906821444538, 113.66087444199421, 113.52466778528103, 113.3806552710153, 113.2295169994472, 113.07140263298271, 112.90599680882947, 112.73288345266148, 112.5516408007249, 112.3613739604525, 112.15988145322208, 111.94715844920572, 111.72388257040038, 111.49464301009483, 111.25898755608783, 111.01423580307016, 110.75921758256436, 110.49022914966581, 110.20651936993757, 109.90596136682568, 109.58956377211187, 109.2629356889294, 108.92306482052436, 108.57209518681937, 108.20796488509167, 107.83070246436095, 107.44109315723959, 107.03784977255091, 106.6258615100051, 106.20534619771772, 105.77311569479299, 105.331029695011, 104.8842761262788, 104.43182307486896, 103.97281246233412, 103.50683104333575, 103.03045745927737, 102.5437792453807, 102.04972006850294, 101.54314611628456, 101.01893184498753, 100.47347244364279, 99.9116317812473, 99.34483949553753, 98.77982659950578, 98.21609254710413, 97.64430905662022, 97.05676407499303, 96.44607397650215, 95.81704222274396, 95.17746835510187, 94.53901789829816, 93.90491062932094, 93.27783457010733, 92.6581462966101, 92.03220051597998, 91.3820221309281, 90.69522896072912, 89.97305496668487, 89.2305002855101, 88.49277821306397, 87.77240022465907, 87.0845103653809, 86.42689250984921, 85.7819658537025, 85.14055325470694, 84.50033094283842, 83.8508175678738, 83.18754110073226, 82.50555998739651, 81.8089697438873, 81.10937192242766, 80.42585531097062, 79.75697467299139, 79.103477516127, 78.46432781211549])
# 1) 4th derivative of B0_col and B0_row using central difference method
CD4_B0_col = np.zeros( (len(B0_col),1) )
for i in range (0, len(B0_col)):
if i == 0:
CD4_B0_col[i] = abs ( B0_col [i+1] - B0_col [i] )
elif i == 1 or i == len(B0_col)-2 :
CD4_B0_col[i] = abs ( B0_col [i+1] - 2 * B0_col [i] + B0_col [i-1] )
elif i > 1 and i < len(B0_col)-2 :
CD4_B0_col[i] = abs ( B0_col [i+2] - 4 * B0_col [i+1] + 6 * B0_col [i] - 4 * B0_col [i-1] + B0_col [i-2] )
else:
CD4_B0_col[i] = abs ( B0_col [i-1] - B0_col [i] )
然而,MATLAB CD4_B0_col与python结果不完全相同(np.array_equal(CD4_B0_col,Matlab_CD4_B0_col)返回False):
MATLAB结果(在python中看到):
Matlab_CD4_B0_col = np.array( [0.48469893441867384, 0.0011685083021717446, 0.002588016916433844, 0.00287123316181237, 0.010687247900804664, 0.020554681927933416, 0.008130162108017203, 0.00521254215139777, 0.00960696924501292, 0.028245788739098998, 0.008204562679466676, 0.010007089314726159, 0.011824899882981299, 0.0036859091062382277, 0.004026440721958124, 0.006673710406843725, 0.01226471828250908, 0.0017645044598992854, 0.0004959324751752092, 0.008570987670111663, 0.0072164556572289484, 0.009070289769823603, 0.0024764776216130713, 0.004424328460416405, 0.005555795839455868, 0.0004751315186553029, 0.0021261956978690932, 0.0013722163268283794, 0.0011177841086436047, 0.002736487504094498, 0.00054839914739091, 0.0019014593510462419, 0.002823516438297702, 0.00020171476748487294, 0.0030282525160458817, 0.004366139611491349, 0.0004009006935064008, 0.0037169306665560953, 0.0013812196741014304, 0.0027698941670877275, 0.0013592509358062443, 0.00038872503918696566, 0.0018280193442592463, 0.0031458468615710444, 0.002135012973752737, 0.00023362306494334462, 0.0010966495459285852, 0.001031510388145307, 0.0001802909854831114, 0.0002831380484025203, 0.00022698283845556944, 0.0009349693550291249, 0.0026740911866625083, 0.0012796647615260781, 0.0003118151312833106, 0.0023935542413511257, 0.0017820501115863863, 0.000338348352840967, 0.0007417123517967639, 0.00019686736877133626, 0.0005786291099099117, 0.0020496568583041608, 0.00046084526091760836, 0.00292989441614111, 0.0010552829997294566, 0.00014409743306487144, 0.00014612068925146104, 0.002392141606065934, 0.004258757608994301, 0.0016869528483169915, 9.897617002252446e-05, 0.0031802693986122677, 0.0057703784080445075, 0.0014507079107062282, 0.0014609129873406346, 0.0031304697589291663, 5.258016244624741e-13, 2.2737367544323206e-13, 2.8421709430404007e-13, 6.110667527536862e-13, 6.252776074688882e-13, 2.8421709430404007e-13, 2.984279490192421e-13, 7.815970093361102e-13, 6.679101716144942e-13, 1.9895196601282805e-13, 1.2789769243681803e-13, 5.542233338928781e-13, 4.263256414560601e-13, 2.842170943040401e-14, 8.526512829121202e-14, 3.268496584496461e-13, 2.842170943040401e-14, 1.9895196601282805e-13, 1.4210854715202004e-13, 5.684341886080802e-14, 3.410605131648481e-13, 4.263256414560601e-13, 2.1316282072803006e-13, 2.8421709430404007e-13, 6.821210263296962e-13, 7.531752999057062e-13, 2.7000623958883807e-13, 1.7053025658242404e-13, 5.968558980384842e-13, 7.247535904753022e-13, 3.836930773104541e-13, 2.8421709430404007e-13, 5.968558980384842e-13, 5.826450433232822e-13, 2.5579538487363607e-13, 3.694822225952521e-13, 5.968558980384842e-13, 5.258016244624741e-13, 2.2737367544323206e-13, 1.7053025658242404e-13, 1.5631940186722204e-13, 5.684341886080802e-14, 2.2737367544323206e-13, 9.947598300641403e-14, 1.2789769243681803e-13, 4.689582056016661e-13, 5.684341886080801e-13, 2.7000623958883807e-13, 3.552713678800501e-13, 7.247535904753022e-13, 6.821210263296962e-13, 2.984279490192421e-13, 2.984279490192421e-13, 6.252776074688882e-13, 7.105427357601002e-13, 3.552713678800501e-13, 2.984279490192421e-13, 5.684341886080801e-13, 4.973799150320701e-13, 2.4158453015843406e-13, 2.1316282072803006e-13, 3.979039320256561e-13, 2.1316282072803006e-13, 1.8474111129762605e-13, 1.5631940186722204e-13, 2.842170943040401e-14, 2.7000623958883807e-13, 2.984279490192421e-13, 1.7053025658242404e-13, 1.4210854715202004e-13, 3.552713678800501e-13, 4.121147867408581e-13, 2.2737367544323206e-13, 9.947598300641403e-14, 2.984279490192421e-13, 4.547473508864641e-13, 2.5579538487363607e-13, 3.552713678800501e-13, 5.258016244624741e-13, 5.400124791776761e-13, 2.5579538487363607e-13, 1.5631940186722204e-13, 2.984279490192421e-13, 3.410605131648481e-13, 9.947598300641403e-14, 1.9895196601282805e-13, 2.1316282072803006e-13, 5.684341886080802e-14, 2.1316282072803006e-13, 1.1368683772161603e-13, 5.684341886080802e-14, 3.126388037344441e-13, 3.836930773104541e-13, 2.7000623958883807e-13, 2.8421709430404007e-13, 4.831690603168681e-13, 4.121147867408581e-13, 5.684341886080802e-14, 2.842170943040401e-14, 3.410605131648481e-13, 5.258016244624741e-13, 3.410605131648481e-13, 3.694822225952521e-13, 6.536993168992922e-13, 5.542233338928781e-13, 2.5579538487363607e-13, 1.9895196601282805e-13, 2.984279490192421e-13, 7.105427357601002e-14, 8.526512829121202e-14, 2.842170943040401e-14, 7.105427357601002e-14, 1.7053025658242404e-13, 2.5579538487363607e-13, 1.2789769243681803e-13, 9.947598300641403e-14, 3.979039320256561e-13, 4.263256414560601e-13, 7.105427357601002e-14, 7.105427357601002e-14, 1.9895196601282805e-13, 1.7053025658242404e-13, 1.4210854715202004e-14, 1.5631940186722204e-13, 2.5579538487363607e-13, 1.8474111129762605e-13, 7.105427357601002e-14, 2.1316282072803006e-13, 3.694822225952521e-13, 2.4158453015843406e-13, 2.842170943040401e-14, 4.263256414560601e-14, 1.9895196601282805e-13, 4.263256414560601e-14, 1.1368683772161603e-13, 1.4210854715202004e-14, 7.105427357601002e-14, 0.006933916491490777, 0.007701270600477983, 0.0010901312225257698, 1.716876738555584e-07, 1.7938052465638066e-05, 0.000601763829578772, 0.00018294946744390472, 0.0003784471614949325, 0.0004841365911971707, 0.0010538023020671972, 0.00013534362271627742, 0.0012582579677626882, 0.00031638951695356354, 0.00047307354061842943, 0.0005304378442048119, 0.00046502519833779843, 0.00010071153361934648, 5.68942782308568e-06, 0.00047312881338257284, 0.0013065860549517083, 0.0021966487942535196, 0.0006824518250141409, 0.0039115712917094925, 0.005041405490061379, 0.0022281931079248807, 0.0015102368317059245, 0.0025335764271261496, 0.002952610467659156, 0.0013757421170339512, 0.003135508335731174, 0.004600471351508872, 0.008621399887189796, 0.005156316676490746, 0.004205922645397209, 0.002090451742489563, 0.0007566835927264037, 0.0020724237890448194, 0.0061763908868357476, 0.004671371594568541, 0.003405969011410548, 0.00504783467582115, 0.0033282341270108873, 0.006219841634532486, 0.0001738352801368137, 0.00044483310885823357, 0.0030081132579056202, 0.003508893818178649, 0.002836131635447714, 0.008057479216745378, 8.268621442653057e-06, 0.0015207327689665817, 0.008468679966014747, 0.006565768739562827, 0.004698624744222002, 0.007231559040221214, 0.008827735664411307, 0.0016162286512297896, 0.0003284273406052307, 0.012187087316846146, 0.0029960797870103306, 0.0038659833388976494, 0.008445747734171505, 0.0005317550509857938, 0.002331445984424363, 0.014001868801969408, 0.004329804439549889, 0.005592916563529116, 0.013616142027117917, 0.013766175412911252, 0.010213159144399242, 0.012701820546581644, 0.0026325697729276953, 0.017360170465750002, 0.015364678981242719, 0.008403662127705047, 0.0068533722096759675, 0.008157580198997039, 0.0060093211422724835, 0.0004695249364345955, 0.009037070038033335, 0.007506036202272526, 0.007487235730053499, 0.02053402447788244, 0.002192744161789051, 0.0017835358990083705, 0.014347452852874198, 0.6391497040115155])
有谁知道为什么会这样?或者如何解决?
我猜是因为在python和matlab中abs不一样,或者因为乘法不一样。
答案 0 :(得分:1)
您需要更改一行:
CD4_B0_col = np.zeros((len(B0_col), 1))
# should be
CD4_B0_col = np.zeros(len(B0_col))
示例:
In[3]: CD4_B0_col = np.zeros((len(B0_col), 1))
...: for i in range(0, len(B0_col)):
...: if i == 0:
...: CD4_B0_col[i] = abs(B0_col[i + 1] - B0_col[i])
...: elif i == 1 or i == len(B0_col) - 2:
...: CD4_B0_col[i] = abs(B0_col[i + 1] - 2 * B0_col[i] + B0_col[i - 1])
...: elif i > 1 and i < len(B0_col) - 2:
...: CD4_B0_col[i] = abs(B0_col[i + 2] - 4 * B0_col[i + 1] + 6 * B0_col[i] - 4 * B0_col[i - 1] + B0_col[i - 2])
...: else:
...: CD4_B0_col[i] = abs(B0_col[i - 1] - B0_col[i])
...:
In[4]: CD4_B0_col.shape
Out[4]: (300, 1)
In[5]: Matlab_CD4_B0_col.shape
Out[5]: (300,)
In[6]: np.array_equal(CD4_B0_col, Matlab_CD4_B0_col)
Out[6]: False
In[7]: CD4_B0_col = np.zeros(len(B0_col)) # <-- fixed
...: for i in range(0, len(B0_col)):
...: if i == 0:
...: CD4_B0_col[i] = abs(B0_col[i + 1] - B0_col[i])
...: elif i == 1 or i == len(B0_col) - 2:
...: CD4_B0_col[i] = abs(B0_col[i + 1] - 2 * B0_col[i] + B0_col[i - 1])
...: elif i > 1 and i < len(B0_col) - 2:
...: CD4_B0_col[i] = abs(B0_col[i + 2] - 4 * B0_col[i + 1] + 6 * B0_col[i] - 4 * B0_col[i - 1] + B0_col[i - 2])
...: else:
...: CD4_B0_col[i] = abs(B0_col[i - 1] - B0_col[i])
...:
In[8]: CD4_B0_col.shape
Out[8]: (300,)
In[9]: Matlab_CD4_B0_col.shape
Out[9]: (300,)
In[10]: np.array_equal(CD4_B0_col, Matlab_CD4_B0_col)
Out[10]: True