slicing a 500 element array into a 20 element array

 

I am trying to run a loop on a 500 element array.

The loop should take elements 480 to 499 then store then in a 20 element array (past) then elements 479 to 498 etc. Basically it start with the last 20 elements in the returns array then shifts back 1 for the next 20 elements and so on.

For some reason i keep getting an error when i populate the past array. Any ideas??

   int jj,nn = 500,nm = 20;
   double returns[],past[];

   int OnInit()
   {
      ArrayResize(past,nm);ArrayResize(returns,nn);
      return(INIT_SUCCEEDED);
   }

   //populate returns array

   for(jj=nn-2;jj>nm;jj--)
   {
      for(kk=jj;kk>(jj-nm);kk--)
      {
         past[nm-1+kk-jj]= returns[kk];
      }
      //next section
   }
 
Lerato Silokwane:

I am trying to run a loop on a 500 element array.

The loop should take elements 480 to 499 then store then in a 20 element array (past) then elements 479 to 498 etc. Basically it start with the last 20 elements in the returns array then shifts back 1 for the next 20 elements and so on.

For some reason i keep getting an error when i populate the past array. Any ideas??

OK, first off you need to correct a couple of things:

  • You're initializing "jj" to 498. What's the reason for that? If you have 500 elements the index goes from 0 to 499. Why do you start at 498?
  • Then you're not slicing in chunks of 20. What you're doing is an outer "for" that goes down 1 by 1, starting at 498 (why? see previous point), then going down (j--). Then you have the inside "for" where you try to populate the array of 20 elements. So in conclusion you're trying to populate the 20-element array some 478 times (jj > nm, or 498 > 20).
You need to correct that first, and your code will work. Trying to figure out what's going out right now, is trying to correct a code that's intrinsically wrongly done. No offense, pal - I'm just trying to help you, but the first step is to get the logic right in those nested loops.
 

I think what you're trying to do is the following (not compiled nor tested, just typed):

for(int i = 499; i >= 0; i-=20)
        for(int j = 0; j <= 19; j++)
                past[j] = returns[i - j];

But still... you have to do something with that array before the next cycle of the outer loop, otherwise you're just overwriting the "past" array over and over. Let me know if it works.

 

A better version of the previous nested loop might be... (again, not tested, just typed):

int nn = 500, nm = 20;
double returns[], past[25][];

int OnInit() {
        ArrayResize(past[25], nm);
        ArrayResize(returns, nn);
        return(INIT_SUCCEEDED);
}

for(int i = 499, loop = 0; i >= 0; i-=20, loop++)
        for(int j = 0; j <= 19; j++)
                past[loop][j] = returns[i - j];
 
Carlos Moreno Gonzalez #:

OK, first off you need to correct a couple of things:

  • You're initializing "jj" to 498. What's the reason for that? If you have 500 elements the index goes from 0 to 499. Why do you start at 498?
  • Then you're not slicing in chunks of 20. What you're doing is an outer "for" that goes down 1 by 1, starting at 498 (why? see previous point), then going down (j--). Then you have the inside "for" where you try to populate the array of 20 elements. So in conclusion you're trying to populate the 20-element array some 478 times (jj > nm, or 498 > 20).
You need to correct that first, and your code will work. Trying to figure out what's going out right now, is trying to correct a code that's intrinsically wrongly done. No offense, pal - I'm just trying to help you, but the first step is to get the logic right in those nested loops.

I added a bit more code for some context. Starting from 498 is intended.  In the rest of the code, returns[480:499] which is a 20 element long array is stored as "today". I want to compare it to other slices. ie. "past" =  returns[479:498], returns[478:497], etc. Maybe 'slice' is the wrong word to use. The data I'm storing in "past" is overlapping pieces of "returns", dropping and adding 1 data point as we move through jj. Not 25 slices of 20 data points. That's why its repeated 478 times. 

   int jj,nn = 500,nm = 20;
   double returns[],past[];

   int OnInit()
   {
      ArrayResize(past,nm);ArrayResize(returns,nn);
      return(INIT_SUCCEEDED);
   }   

   //populate returns
   
   double today[nm];
   for(jj=(nn-1);jj>(nn-nm);jj--)
   {
      today[jj-nn+nm]= returns[jj];
   } 

   for(jj=nn-2;jj>nm;jj--)
   {
      for(kk=jj;kk>(jj-nm);kk--)
      {
         past[kk-jj+nm-1]= returns[kk];
      }
      MathCorrelationPearson(today,past,cor);
      //next section
   }