MATLAB Answers

Python function to Matlab

3 ビュー (過去 30 日間)
david crowley
david crowley 2021 年 4 月 27 日
回答済み: Anagha Mittal 2021 年 6 月 17 日
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])

回答 (1 件)

Anagha Mittal
Anagha Mittal 2021 年 6 月 17 日
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!

Translated by