I have a 429×1 vector that represents a hydrological time series. I am looking to “lag” the time series by a time step and turn it into a matrix for input into the nftool for some ANN analysis. The width of the matrix is controlled by the amount of input neurons in my input layer, which is a value I read in from a spread sheet. This is what I would like to do using a shorter time series to illustrate the example:
inp_neur = 5; % amount of input neurons (read in from excel)
A = [9;6;8;3;2]; % hypothetical hydrological time series
% do pad zero process
RESULT:
newA =
9 0 0 0 0
6 9 0 0 0
8 6 9 0 0
3 8 6 9 0
2 3 8 6 9
I’m sure this isn’t the hardest thing to do, but can it be done in a one liner?
Any help would be greatly appreciated.
Cheers,
JQ
Another example with inp_neur = 7;
A = [11;35;63;21;45;26;29;84;51]
newA =
11 0 0 0 0 0 0
35 11 0 0 0 0 0
63 35 11 0 0 0 0
21 63 35 11 0 0 0
45 21 63 35 11 0 0
26 45 21 63 35 11 0
29 26 45 21 63 35 11
84 29 26 45 21 63 35
51 84 29 26 45 21 63
I know that this question has already been marked accepted, however, I think it is worth pointing out that the current accepted answer will be very inefficient if
T(the number of observations in the time series) is much larger thanK(the number of lags, ieinp_neurin the OP’s notation). This is because it creates aTbyTmatrix then truncates it toTbyK.I would propose two possible alternatives. The first uses a function from the Econometrics toolbox designed to do exactly what the OP wants:
lagmatrix. The second is a loop based solution.The
lagmatrixsolution returnsNaNwhere the OP wants0, so an additional line is necessary to convert them. The full solution is:The loop based solution is:
The obvious advantage of the loop based solution is that it does not require the econometrics toolbox. But is that the only advantage? Let’s try some timed runs. Set
T = K = 10. Then:3lectrologos solution and the
lagmatrixsolution are essentially the same. The loop based solution is 3 times faster! Now, to emphasize the problem with 3lectrologos solution, setT = 1000andK = 10. Then:Now 3lectrologos solution is two orders of magnitude slower than the
lagmatrixsolution. But the real winner on the day is the loop based solution that still manages to be 3 times faster than thelagmatrixsolution.Conclusion: Don’t discount single-loops in Matlab anymore. They are getting really fast!
For those who are interested, the code for the timed runs is below: