MATLAB Answers

Python function to Matlab

3 views (last 30 days)
david crowley
david crowley on 27 Apr 2021
Answered: Anagha Mittal on 17 Jun 2021
I have the following moving average function developed in Python3 that I need to convert into MATLAB. However, I can't seem to find the correct syntax.
What is the MATLAB syntax to do the following? Please help.
#defines the moving average function
def ma(df, window_size):
return df.groupby('ticker')['close'].apply(lambda x:x.rolling(center=False,window=window_size).mean())
#defines the periods for the moving average calculations
def ma_periods(df, months):
for month in months:
df['{}mo_ma'.format(month)] = ma(monthly_raw_data, month)
return df
#specify the moving average periods to calculate
periods = ma_periods(monthly_raw_data, [6,9,12])

Answers (1)

Anagha Mittal
Anagha Mittal on 17 Jun 2021
MATLAB provides an inbuilt function called 'movavg' for calculating the moving average of any data.
Here is the documentation for reference: https://www.mathworks.com/help/finance/movavg.html

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!