필터 지우기
필터 지우기

Compute operation with different index (part II)

조회 수: 1 (최근 30일)
Attilio Pittelli
Attilio Pittelli 2021년 9월 27일
답변: Walter Roberson 2021년 9월 27일
Good morning,
i've this table and i would like to build a function, where i could insert the time series as input and another input, an integer "n", that has a function of a loockback period.
example: Adj_Close(today) - Adj_Close(lookback) and i would like to apply to the all time series
example data in the attchment
  댓글 수: 2
Image Analyst
Image Analyst 2021년 9월 27일
Give a small example with actual data and show what the output should look like.
Attilio Pittelli
Attilio Pittelli 2021년 9월 27일
the output as i wrote before should be
AdjClose(today) - AdjClose(lookback)
then
AdjClose(lookback) - AdjClose(2*lookback)
and so on

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 27일
It would be easier if you would switch over to table or timetable instead of using timeseries .
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/750909/AAPL.csv';
t = readtable(filename, 'VariableNamingRule', 'preserve');
tt = table2timetable(t);
ts = timeseries(tt);
ts
timeseries Common Properties: Name: 'unnamed' Time: [251x1 double] TimeInfo: tsdata.timemetadata Data: [251x6 timetable] DataInfo: tsdata.datametadata
n = randi(10)
n = 8
result = a_function(ts, n);
result
timeseries Common Properties: Name: 'unnamed' Time: [243x1 double] TimeInfo: tsdata.timemetadata Data: [243x1 double] DataInfo: tsdata.datametadata
function result = a_function(ts, n)
T = ts.Time(n+1:end);
adj = ts.Data.('Adj Close');
result = timeseries(adj(n+1:end) - adj(1:end-n), T);
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by