Rolling window column selection
Show older comments
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
- 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?
Answers (2)
Adam Danz
on 24 Sep 2018
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
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
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!