Rolling window column selection

2 次查看(过去 30 天)
pavlos
pavlos 2018-9-23
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 个评论
Adam Danz
Adam Danz 2018-9-24
编辑:Adam Danz 2018-9-24
  • 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?

请先登录,再进行评论。

回答(2 个)

Adam Danz
Adam Danz 2018-9-24
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 2018-9-24
编辑:Andrei Bobrov 2018-9-24
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by