Rolling window column selection

2 visualizzazioni (ultimi 30 giorni)
pavlos
pavlos il 23 Set 2018
Modificato: Andrei Bobrov il 24 Set 2018
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 Commento
Adam Danz
Adam Danz il 24 Set 2018
Modificato: Adam Danz il 24 Set 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?

Accedi per commentare.

Risposte (2)

Adam Danz
Adam Danz il 24 Set 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
Andrei Bobrov il 24 Set 2018
Modificato: Andrei Bobrov il 24 Set 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

Community Treasure Hunt

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

Start Hunting!

Translated by