0

I'm trying to convert a code written in MATLAB to python script. I don't much of prior knowledge in MATLAB, so I'm unable to figure out what this code is doing and how to convert it to python.

sumint=[];
for k=1:1:length(d)-(tint-2)
    sumint=[sumint;sum(d(k:k+(tint-2)))]
end

I've tried some combinations of function but the output is not matching

My code

sum_disp = []
for i in range(0, len(disp) - (points_grouped - 2)):
    sum_disp.append(sum(disp[i:(i + (points_grouped - 1))]))

sum_disp = np.array(sum_disp)

Can someone please tell me how to covert that piece of code from MATLAB to python

d = disp
tint = points_grouped
sumint = sum_disp

d = [0.02801167 0.03669624 0.0272492  0.0208854  0.0082905  0.01288594
 0.01017967 0.01465445 0.01436482 0.05004975 0.0062021  0.02299043
 0.02368145 0.006786   0.02977529 0.01532477 0.02310713 0.02113559
 0.03423545 0.01603583 0.03599799 0.01562202 0.05216167 0.04165677
 0.01075509 0.01963266 0.03076811 0.02368203 0.04459419 0.01227608
 0.01554692 0.01537159 0.01947022 0.01487557 0.00755353 0.02223923
 0.03342209 0.04716421 0.02321706 0.03620767 0.01531807 0.02143145
 0.07581067 0.09864071 0.06802934 0.08344471 0.04110631 0.02693593
 0.01805178 0.02831497 0.02184237 0.02761051 0.03136386 0.02887697
 0.03444354 0.0232088  0.04235497 0.03862241 0.0228297  0.03749918
 0.03118549 0.01920405 0.04887996 0.03679627 0.03127032 0.03119164
 0.00888661 0.01037151 0.03510487 0.09559838 0.0715161  0.06366703
 0.04879124 0.05652408 0.08160136 0.0707258  0.10876558 0.06095913
 0.06669257 0.14134084 0.11763063 0.08965415 0.06882186 0.11428816
 0.09198447 0.04850028 0.03025621 0.02059732 0.02590883 0.00680715
 0.01868523 0.01118559 0.01455745 0.04500167 0.01250587 0.02199164
 0.0175176  0.02161969 0.00989601 0.07096723 0.05321957 0.07031943
 0.06077753 0.0315616  0.07922844 0.03241386 0.04955126 0.04118749
 0.07775704 0.03544656 0.02747782 0.01317484 0.0086493  0.0066854
 0.03952626 0.04835286 0.04847035 0.04397535 0.0723641  0.12843771
 0.06867013 0.51434501]

MATLAB output

sumint =

    0.0920
    0.0848
    0.0564
    0.0421
    0.0314
    0.0377
    0.0392
    0.0791
    0.0706
    0.0792
    0.0529
    0.0535
    0.0602
    0.0519
    0.0682
    0.0596
    0.0785
    0.0714
    0.0863
    0.0677
    0.1038
    0.1094
    0.1046
    0.0720
    0.0612
    0.0741
    0.0990
    0.0806
    0.0724
    0.0432
    0.0504
    0.0497
    0.0419
    0.0447
    0.0632
    0.1028
    0.1038
    0.1066
    0.0747
    0.0730
    0.1126
    0.1959
    0.2425
    0.2501
    0.1926
    0.1515
    0.0861
    0.0733
    0.0682
    0.0778
    0.0808
    0.0879
    0.0947
    0.0865
    0.1000
    0.1042
    0.1038
    0.0990
    0.0915
    0.0879
    0.0993
    0.1049
    0.1169
    0.0993
    0.0713
    0.0504
    0.0544
    0.1411
    0.2022
    0.2308
    0.1840
    0.1690
    0.1869
    0.2089
    0.2611
    0.2405
    0.2364
    0.2690
    0.3257
    0.3486
    0.2761
    0.2728
    0.2751
    0.2548
    0.1707
    0.0994
    0.0768
    0.0533
    0.0514
    0.0367
    0.0444
    0.0707
    0.0721
    0.0795
    0.0520
    0.0611
    0.0490
    0.1025
    0.1341
    0.1945
    0.1843
    0.1627
    0.1716
    0.1432
    0.1612
    0.1232
    0.1685
    0.1544
    0.1407
    0.0761
    0.0493
    0.0285
    0.0549
    0.0946
    0.1363
    0.1408
    0.1648
    0.2448
    0.2695
    0.7115

2 Answers2

1

Try:

import numpy as np

d = np.array(d)

sumint=np.empty(0)
for k in range(d.size-(tint-2)):
    sumint=np.hstack((sumint, np.sum(d[k:k+(tint-2)])))

output:

[0.09195711 0.08483084 0.0564251  0.04206184 0.03135611 0.03772006
 0.03919894 0.07906902 0.07061667 0.07924228 0.05287398 0.05345788
 0.06024274 0.05188606 0.06820719 0.05956749 0.07847817 0.07140687
 0.08626927 0.06765584 0.10378168 0.10944046 0.10457353 0.07204452
 0.06115586 0.0740828  0.09904433 0.0805523  0.07241719 0.04319459
 0.05038873 0.04971738 0.04189932 0.04466833 0.06321485 0.10282553
 0.10380336 0.10658894 0.0747428  0.07295719 0.11256019 0.19588283
 0.24248072 0.25011476 0.19258036 0.15148695 0.08609402 0.07330268
 0.06820912 0.07776785 0.08081674 0.08785134 0.09468437 0.08652931
 0.10000731 0.10418618 0.10380708 0.09895129 0.09151437 0.08788872
 0.0992695  0.10488028 0.11694655 0.09925823 0.07134857 0.05044976
 0.05436299 0.14107476 0.20221935 0.23078151 0.18397437 0.16898235
 0.18691668 0.20885124 0.26109274 0.24045051 0.23641728 0.26899254
 0.32566404 0.34862562 0.27610664 0.27276417 0.27509449 0.25477291
 0.17074096 0.09935381 0.07676236 0.0533133  0.05140121 0.03667797
 0.04442827 0.07074471 0.07206499 0.07949918 0.05201511 0.06112893
 0.0490333  0.10248293 0.13408281 0.19450623 0.18431653 0.16265856
 0.17156757 0.1432039  0.16119356 0.12315261 0.16849579 0.15439109
 0.14068142 0.07609922 0.04930196 0.02850954 0.05486096 0.09456452
 0.13634947 0.14079856 0.1648098  0.24477716 0.26947194]
Ehsan
  • 12,072
  • 2
  • 20
  • 33
1

At its simplest it is a row separator:

>> [1,2;3,4]        # octave
ans =

   1   2
   3   4

The np.matrix subclass copies it with its string syntax:

In [1005]: np.matrix('1,2;3,4')                                                                        
Out[1005]: 
matrix([[1, 2],
        [3, 4]])

More conventional numpy input:

In [1006]: np.array([[1,2],[3,4]])                                                                     
Out[1006]: 
array([[1, 2],
       [3, 4]])

===

In MATLAB matrices are inherently column oriented 2d:

>> sumint=[]
sumint = [](0x0)
>> sumint=[sumint; 12]
sumint =  12
>> sumint=[sumint; 12]
sumint =

   12
   12

>> sumint=[sumint; 12]
sumint =

   12
   12
   12

>> size(sumint)
ans =

   3   1

So the repeated x = [x; y] just concatenates on a new new value - in the first dimension. Using , instead, joins horizontally:

>> sumint=[]
sumint = [](0x0)
>> sumint=[sumint, 12]
sumint =  12
>> sumint=[sumint, 12]
sumint =

   12   12

>> size(sumint)
ans =

   1   2

A python equivalent is list append:

In [1007]: alist = []                                                                                  
In [1008]: alist.append(12)                                                                            
In [1009]: alist.append(12)                                                                            
In [1010]: alist.append(12)                                                                            
In [1011]: alist                                                                                       
Out[1011]: [12, 12, 12]

You could do repeated concatenates with numpy arrays, but it's inefficient and generally discouraged. It ok in MATLAB simply because it does a fair amount of JIT compiling. In older MATLABs it would have been frowned upon.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 2
    Repeated concatenation is still discouraged in MATLAB. Repeated appending is too, but that is not as slow as repeated concatenation. The speed is not because of the JIT, it is because MATLAB doubles the allocated memory for an array when appending to it. See [here](https://stackoverflow.com/q/48351041/7328782) (both Q and A) for an experiment that demonstrates the difference between repeated concatenation and repeated appending. – Cris Luengo Apr 28 '20 at 23:17