Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:41:41+00:00 2026-06-16T01:41:41+00:00

I have a 429×1 vector that represents a hydrological time series. I am looking

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:41:42+00:00Added an answer on June 16, 2026 at 1:41 am

    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 than K (the number of lags, ie inp_neur in the OP’s notation). This is because it creates a T by T matrix then truncates it to T by K.

    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 lagmatrix solution returns NaN where the OP wants 0, so an additional line is necessary to convert them. The full solution is:

    newA2 = lagmatrix(A, 0:K-1);
    newA2(isnan(newA2)) = 0;
    

    The loop based solution is:

    newA3 = zeros(T, K);
    for k = 1:K
        newA3(k:end, k) = A(1:end-k+1);
    end
    

    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:

    Elapsed time is 0.045809 seconds. %# 3lectrologos solution
    Elapsed time is 0.049845 seconds. %# lagmatrix solution
    Elapsed time is 0.017340 seconds. %# loop solution
    

    3lectrologos solution and the lagmatrix solution are essentially the same. The loop based solution is 3 times faster! Now, to emphasize the problem with 3lectrologos solution, set T = 1000 and K = 10. Then:

    Elapsed time is 10.615298 seconds.
    Elapsed time is 0.149164 seconds.
    Elapsed time is 0.056074 seconds.
    

    Now 3lectrologos solution is two orders of magnitude slower than the lagmatrix solution. But the real winner on the day is the loop based solution that still manages to be 3 times faster than the lagmatrix solution.

    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:

    M = 1000; %# Number of iterations for timed test
    T = 1000; %# Length of your vector of inputs
    K = 10; %# Equivalent to your inp_neur
    A = randi(20, T, 1); %# Generate random data
    
    %# 3lectrologos solution (inefficient if T is large relative to K)
    tic
    for m = 1:M
        tmp = tril(toeplitz(A));
        newA1 = tmp(:, 1:K);
    end
    toc
    
    %# lagmatrix solution
    tic
    for m = 1:M
        newA2 = lagmatrix(A, 0:K-1);
        newA2(isnan(newA2)) = 0;
    end
    toc
    
    %# Loop based solution
    tic
    for m = 1:M
        newA3 = zeros(T, K);
        for k = 1:K
            newA3(k:end, k) = A(1:end-k+1);
        end
    end
    toc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like: <sizes> <size name=original width=386 height=429> http://userserve-ak.last.fm/serve/_/62380831/Adele+PNG.png
I have a list of 429 MFC resource files that I have to generate
have written this little class, which generates a UUID every time an object of
I have a simple class library that I use in Excel. Here is a
I have a lot of time information in the format of hh:mm and I
Have a photography site that I want to prevent image copying from. How can
I have a VB6 COM DLL that was developed on a WinXP 32bit system.
I have a nested inner class that extends AsyncTask to run a db query
I have having trouble with retrieving chatmessages in winjs.xhr: function getMessage() { var time
I have a messages table that auto increments and I simply want to able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.