Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Matlab Help!

  • 20-08-2009 10:10am
    #1
    Posts: 5,589 ✭✭✭


    I am working on a piece of matlab code where I have a row vector
    containing data generated from a looped process.
    I want to evaluate some of that data but only over a small subset, ie, the last 5 values. However, I keep running into
    errors for the first five periods as it is saying that i-5 doesn't
    exist! Do you know of a way to get around this and not have to hard
    code in the initial 5 periods?


Comments

  • Registered Users, Registered Users 2 Posts: 225 ✭✭CathalMc


    Some options:
    • Code the first 5 values outside the loop, then start at i=6.
    • Exception code inside the loop (overly complicated)
    • If the first 5 are calculated simply before your data range (ie: i=-4, -3 ... 0) then simply initialize your vector with these values, and start the loop at i=6 (corresponding to i=1 in your previous code).


  • Posts: 5,589 ✭✭✭ [Deleted User]


    Yeah, its the middle one that I probably need as I want to be able to change the size of the subset.

    I've done this for stata, but through a monumental ****up of a backup I lost all my do files from that course.


  • Registered Users, Registered Users 2 Posts: 225 ✭✭CathalMc


    Thats messy. If you post some code or pseudocode here I can have a look if you like. Or PM me if its ultra-secretive stuff.


  • Posts: 5,589 ✭✭✭ [Deleted User]


    I'm thinking perhaps:
    Let say this subset is k obs long, and that I want to find the mean

    then in the middle of the loop:
    if i < k
    x = mean(g);
    else
    x = mean(trunc_g);
    end

    where trunc_g is the k period subset of g

    does that look ok?


  • Registered Users, Registered Users 2 Posts: 225 ✭✭CathalMc


    Well if it can be simplified that easily:

    for i=1:N
    j = max((i-4),1);
    x = mean(g[j:i]);
    end

    I may have misunderstood your "k period subset of g", " k obs" phrases, I'm unfamiliar with this lingo.


  • Advertisement
  • Posts: 5,589 ✭✭✭ [Deleted User]


    Basically, g is vector the spans the whole period under review with n observations.

    I want to now run a function, like get the mean of a subset of g, lets call it trunc_g which consists of k observations, where k is some number such that k < n.

    Both g and trunc_g are generated from a loop process so at each period g(i) and trunc_g(i) are calculated and the loop runs from i=2 -> i=n.

    The problem is that for the first few periods, call it j (where j < k) I get errors on trunc_g as its trying to use parameters that don't exist. Hence the need for an escape clause that runs some other function while i is an element of j.

    Does that make sense? I suck at the explaining things!


  • Registered Users, Registered Users 2 Posts: 225 ✭✭CathalMc


    Code is so much more concise...

    Well it depends on what you are doing with that data - if you can operate on it simply as subvectors (operations on g(j:i) that are agnostic of its length), then you can simply drop in my code - otherwise you are condemned to some lengthy if/else fun.


  • Posts: 5,589 ✭✭✭ [Deleted User]


    Yeah, this laptop doesn't have matlab so looks like fun evening when I get home!


  • Posts: 5,589 ✭✭✭ [Deleted User]


    Well the escape code worked!

    However, my code for making a vector consisting of the last k observations from another vector failed. Horribly.

    Anyone got any ideas?


  • Closed Accounts Posts: 10,012 ✭✭✭✭thebman


    for anyone writing code especially if you've lost some of your own past samples on how to do things, sites like http://github.com/ might help.

    Search already written code that might help you.

    Same with http://www.koders.com/ which also allows you to specify Matlab as the language.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 225 ✭✭CathalMc


    You might as well post in the code zaraba, otherwise we're speculating in the dark here - and you may have just made a simple error.


  • Posts: 5,589 ✭✭✭ [Deleted User]


    test(i) = mean(vec(1,i-k:i))

    sorted it


Advertisement