0

I need to translate this matlab code

result = movmean(std_new1,PTA); 

into numpy.

std_new1 is a 1x19290 vector as follows:

std_new1 = NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.0223287, 0.023921, 0.0255133, 0.0271056, 0.0286979, 0.0302902, ....

and PTA is 1x1 vector = 20

I tried numpy:

result = np.ma.average(std_new1, PTA)

but I get TypeError: only integer scalar arrays can be converted to a scalar index

I then tried also

result = np.ma.average(std_new1, PTA[0])

but then i get IndexError: tuple index out of range

I'm reading numpy doc and it says

numpy.ma.average(a, axis=None, weights=None, returned=False)

so i don't understand what is the issue. First code should be fine but it isn't.

Any better alternative to calculate moving average?

Thank you

Adriaan
  • 17,741
  • 7
  • 42
  • 75
claw91
  • 161
  • 4
  • 11
  • Does this answer your question? [Moving average or running mean](https://stackoverflow.com/questions/13728392/moving-average-or-running-mean) – DavidG Jun 08 '20 at 10:01
  • hello, yes I had already found that but there is a comment in the solution saying that it is orders of magnitude slower than the solutions with Numpy... so I thought numpy has a much easier and optimized way of achieving result with 1 line of code. I'm working with huge matrices so I don't want Pycharm to go into a crawl (which already does btw) – claw91 Jun 08 '20 at 10:14
  • Did you look at the other answers in that question? e.g. https://stackoverflow.com/a/27681394/5851928 – DavidG Jun 08 '20 at 10:22
  • yes but i get very different results – claw91 Jun 08 '20 at 10:37
  • i found out that std_new has got 9 NA values while Python fills those NA with the same value (0.022329)... maybe that is the cause – claw91 Jun 08 '20 at 11:09

0 Answers0