Rolling window column selection

Hello,
Please help me with the following:
Consider a 365x1 matrix A.
I need to extract the new column vector as:
v1: the first 48 values 1-48
v2: the next 48 values 25-73
v3: the next 48 values 49-97
That means that the next vector has the last 24 of the previous vector plus the next 24.
Can this be done with a loop or other method?
Thank you.
Best,
Pavlos

1 Comment

Adam Danz
Adam Danz on 24 Sep 2018
Edited: Adam Danz on 24 Sep 2018
  • 1:48 has 47 values.
  • 25:73 has 48 values
  • 49:97 has 48 values.
Did you mean for the first example to start with 0
  • 0:48 has 48 values.
Also, since 365 isn't divisible by 24, is it OK that the last ~5 elements of data won't be represented in any of the new column vectors?

Sign in to comment.

Answers (2)

Here's one solution that creates 14 column vectors in a manner your described. I stored fake data in column v, identified the start and stop indices of each new column, and then looped through each stop index to create a new matrix vSplit where each column contains a subsection of v.
If it turns out that each new column of data is not of equal length, you'll need to store the data in a cell array rather than a matrix.
v = rand(365, 1);
startIdx = 1:24:length(v);
stopIdx = 48:24:length(v);
vSplit = zeros(48, length(stopIdx));
for i = 1:length(stopIdx)
vSplit(:,i) = v(startIdx(i):stopIdx(i));
end
Andrei Bobrov
Andrei Bobrov on 24 Sep 2018
Edited: Andrei Bobrov on 24 Sep 2018
v = randi(1000,365,1);
out = v((1:24:numel(v) - 48)' + (0:47)); % version of MATLAB >= R2016a
out = v(bsxfun(@plus,(1:24:numel(v) - 48)',0:47)); % < R2016a

Categories

Asked:

on 23 Sep 2018

Edited:

on 24 Sep 2018

Community Treasure Hunt

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

Start Hunting!