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

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일

0 개 추천

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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 9월 23일

편집:

2018년 9월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by