Matrix with sliding window elements

I have a time series, and I apply some user-defined function to each element of W in the time series.

Right now, I'm just using it for a loop, a slide window of size W, applying my function to the elements in the window at each iteration.

I use Matlab and it is very inefficient using "for loops", so I would like to vectorize this operation.

As a solution, I see the conversion of a signal of length N into a matrix with a size of (N - 1, W), where each row represents a time series in different windows and applies a function to this matrix.

So my questions are:

  • How to convert my initial time series to such a matrix?
  • Let's say I have a sliding window with step X. Thus, there will be no (N - 1, W) matrix, but ((N - 1) / X, W). (Each Xth row of the matrix from [1])

Example:

Say my time series:

T = [1, 5, 6, 8, 10, 14, 22]
W = 3
X = 1

=> I would like to receive

[[1, 5, 6], 
[5, 6, 8], 
[6, 8, 10],
[8, 10, 14],
[10, 14, 22]]

If

W = 3
X = 2

=> I would like to receive

[[1, 5, 6], 
[6, 8, 10],
[10, 14, 22]]
+4
source share
2 answers

Creating the right indexes with bsxfunshould certainly help:

ind = bsxfun(@plus, 1:W, (0:X:numel(T)-W).');
out = T(ind);  

Creating the right indexes is the first step indicated by the first line of code. What this code does is that it creates a 2D matrix, where each row is an element to access the window of interest. If you want to get an intuition about how code generates indexes, look specifically at the first case when X = 1;and W = 3;.

, 1, 2, 3. 2, 3, 4... , 5, 6, 7. , , 1, 2, 3 1 W. , T . 0, - 1 , 3. , 1 . 1 , 2 . , , , T.

, X = 2; W = 3;, , 1, 2, 3. 1, 2, 3 , 3, 4, 5 , 5, 6, 7 . 2 1 . 2 , 4 ..

1:W, 0:X:numel(T)-W. W , . , , bsxfun .

1:W, (0:X:numel(T)-W).', . , 0, X , , . , numel(T)-W, . bsxfun, 2D-, , , , , , , .

W = 3; X = 1; :

>> T = [1, 5, 6, 8, 10, 14, 22];
>> X = 1;
>> W = 3;
>> ind = bsxfun(@plus, 1:W, (0:X:numel(T)-W).')

ind =

     1     2     3
     2     3     4
     3     4     5
     4     5     6
     5     6     7

, W = 3; X = 2;, :

>> T = [1, 5, 6, 8, 10, 14, 22];
>> X = 2;
>> W = 3;
>> ind = bsxfun(@plus, 1:W, (0:X:numel(T)-W).')

ind =

     1     2     3
     3     4     5
     5     6     7

, T, .

, , :

out = T(ind);

X = 1; W = 3; :

>> out = T(ind)

out =

     1     5     6
     5     6     8
     6     8    10
     8    10    14
    10    14    22

X = 2; W = 3; :

>> out = T(ind)

out =

     1     5     6
     6     8    10
    10    14    22
+6

Rayryeng , , . . , .

X ( ) y. , "" X, . , 21 [T-2 T-3 T-5 T-8 T-13 T-21] X T y

- y. , X= [T-1 T-2] y= T + 2

, - .

%   get_Sliding_Indexes:
%       Useful for autoregression on a univariate time series.
%       Returns the indexes for the predictor and response variables
%       according to a sliding window.
%
%   Copyright (C) 20016  Florin Schimbinschi
%
%   Parameters:
%       numRecords - the number of records in the dataset
%
%       windowLag - number of past samples to take - it will be equal to 
%           the size of the predictor vector X. Default 10
%
%       predHorizon - the prediction horizon is the number of steps into 
%           the future that predictions are to be made. Default 1
%
%       windowPattern - by default the window will take all consecutive 
%           values in the past over the window lag size, however it is
%           possible to sample using a custom pattern. 
%           For example taking every second value can be done by setting
%           this parameter to 1:2:5. Default 1:windowLag
%
%       stepSize - number of steps taken when window is moved. Default 1
%   
%   Returns:
%       indX - predictor variable indexes
%       indY - response variable indexes
%
%
%      windowPattern = 1:2:9   __ structure between [] is moved to
%             /     \         /   the right by stepSize units
%   >------[(9-7-5-3-1)---(y)]--------------->
%            \_______/ \_/
%         X = [13579]   predHorizon = 3
%
%
%   Example on a multivariate time series (two) with 6 records:
%
%     data2d = [ .1  .2  .3  .4  .5  .6 
%               .11 .22 .33 .44 .55 .66]';
% 
%     [X, y] = getSlidingIndexes(size(data2d,1), 4)
%       X =
%            1     2     3     4
%            2     3     4     5
%       y =
%            5
%            6
%
%     Assuming we are interested in the second series (column):
%
%     series2 = data2d(:,2);
%
%     series2(X)
%     ans =
%         0.1100    0.2200    0.3300    0.4400
%         0.2100    0.3300    0.4400    0.5500
% 
%     series2(y)
%     ans =
%         0.5500
%         0.6600
%
function [indX, indY] = get_Sliding_Indexes(numRecords, ...
                        windowLag, predHorizon, windowPattern, stepSize)

    if ~exist('numRecords','var') || isempty(numRecords)
        error('The number of records in the dataset is not specified');
    end
    if ~exist('stepSize','var') || isempty(stepSize)
        stepSize = 1; % steps taken when moving the window
    end
    if ~exist('predHorizon','var') || isempty(predHorizon) 
        predHorizon = 1; % aiming to predict this many steps in the future
    end  
    if ~exist('windowLag','var') || isempty(windowLag) 
        windowLag = 10; % number of time steps to look back
    end
    if exist('windowLag','var') && (windowLag > numRecords)
        error('The size of the window is larger than the number of observations');
    end
    if ~exist('windowPattern','var') || isempty(windowPattern) 
        windowPattern = 1:windowLag; % pattern of sampling data
    end
    if exist('windowPattern','var') && windowPattern(end) > windowLag
        error('The window pattern must stop at the window lag specified');
    end

    % the number of samples in the window
    maxSample = max(windowPattern);

    indX = bsxfun(@plus, windowPattern, ...
        (0:stepSize:(numRecords - maxSample - predHorizon))');
    indY = bsxfun(@plus, max(windowPattern) + predHorizon, ...
        (0:stepSize:(numRecords - maxSample - predHorizon))');
end

: https://au.mathworks.com/matlabcentral/fileexchange/58730-get-sliding-indexes-numrecords--windowlag--predhorizon--windowpattern--stepsize-

0

All Articles