MATLAB Answers

0

Rolling-window matrix with different intervals between columns.

Asked by farshid balaneji on 13 Jan 2018
Latest activity Answered by Jan
on 11 Jun 2019
Hi,
I have a vector of data for 21 years with daily data and want to create a rolling window of 365 days such as the next period stars one month (30 days) after the previous one, for example:
vec = [1 2 3 4 5 6 7 8 9 10 11 12 13]
for a given variable n_interval = 3, I want to get a matrix output like:
mat = [[1 2 3 4 5],
[4 5 6 7 8],
[7 8 9 10 11],
[10 11 12 13]]
I looked at this question but couldn't apply it to my problem.
Any help would be appreciated

  0 Comments

Sign in to comment.

2 Answers

Answer by Wieszner Vilmos on 11 Jun 2019

Hi,
I think this is what you are looking for:

  0 Comments

Sign in to comment.


Answer by Jan
on 11 Jun 2019

vec = [1 2 3 4 5 6 7 8 9 10 11 12 13];
n = numel(vec);
step = 3;
width = 4;
col1 = 1:step:n - width + 1;
row1 = 0:width - 1;
index = col1.' + row1;
result = vec(index)

  0 Comments

Sign in to comment.