Download MetaTrader 5
To add comments, please log in or register
They are already making profit using the Market. You can join them!
Gordon Gekko
654
Gordon Gekko 2016.09.11 10:49 

Hi gurus,

Is it possible to copy a part of a 2D array into a 1D array?

So, there are:

double 1DArray[100];

double 2DArray[10][100];

I want to copy 2DArray[5][all of the 100 elements] into 1DArray.

Is it possible and how?

Thanks

Keith Watford
Moderator
7908
Keith Watford 2016.09.11 11:55  
  double OneDArray[100];
  double TwoDArray[10][100];
  for(int x=0;x>100;x++)
     OneDArray[x]=TwoDArray[5][x];

.

Gordon Gekko
654
Gordon Gekko 2016.09.11 12:29  
GumRai:

.

OK, thank you, this method is known by me too.

Is there any ArrayCopy-solution as well? (ArrayCopy is faster.)

Fernando Carreiro
1468
Fernando Carreiro 2016.09.11 13:30  
ggekko: OK, thank you, this method is known by me too. Is there any ArrayCopy-solution as well? (ArrayCopy is faster.)

As you may verify on the documentation, almost all of the "Array???()" functions are for single-dimension arrays and not for any greater dimension.

However, there could be hidden, documented ways around the situation, but it would be risky to rely on it, looking to the future.

By this I mean the following - According to the documentation of ArrayFill(), multidimensional arrays are treated as continuous single-dimension arrays, when carrying out the "fill". If I were to assume that the ArrayCopy() function might work in a similar manner (although not mentioned in the documentation), it might be possible to use it in the way you intent. You would however, have to test this to see if it is indeed the case.

In my opinion, even if it does have that hidden feature, it would be best not to rely on it and you should code your own function. Since the MQL code is compiled (and not interpreted), the difference in speed between a native Array function and a MQL coded equivalent, should not be too much. In fact many of the Array Classes developed by MetaQuotes, re-implement many of the Array functions in pure MQL code and don't use built-in functions.

whroeder1
13616
whroeder1 2016.09.11 15:58  
FMIC: Since the MQL code is compiled (and not interpreted), the difference in speed between a native Array function and a MQL coded equivalent, should not be too much.

Since an array can be referenced as series or not, and be enlarged at either end (via asSeries(!c),resize, asSeries(c),) don't assume.

Pre-build 600: each array reference was over ten times slower than a variable reference (by test.)

Build 1010: is it only 4.8 times slower (1359/282) and the array functions are 6.2 times faster (1359/218.)

#property strict
void OnStart(){
{
   uint start = GetTickCount();
   double arr[10000];
   for(int i=0; i < 10000; ++i)
      for(int j=0; j < 10000; ++j)
         arr[j]=i;
   uint end   = GetTickCount();
   PrintFormat("100 million array ref=%i", end - start);
}
{
   uint start = GetTickCount();
   double v;
   for(int i=0; i < 10000; ++i)
      for(int j=0; j < 10000; ++j)
         v=i;
   uint end   = GetTickCount();
   PrintFormat("100 million var ref=%i", end - start);
}
{
   uint start = GetTickCount();
   double arr[10000];
   for(int i=0; i < 10000; ++i)
         ArrayInitialize(arr, i);
   uint end   = GetTickCount();
   PrintFormat("100 million array init=%i", end - start);
}
{
   uint start = GetTickCount();
   double arr[10000];
   for(int i=0; i < 10000; ++i)
         ArrayFill(arr, 0, 10000, i);
   uint end   = GetTickCount();
   PrintFormat("100 million array fill=%i", end - start);
}
}
2016.09.11 11:54:24.530    testscr USDMXN,H4: 100 million array fill=219
2016.09.11 11:54:24.308    testscr USDMXN,H4: 100 million array init=218
2016.09.11 11:54:24.089    testscr USDMXN,H4: 100 million var ref=282
2016.09.11 11:54:23.809    testscr USDMXN,H4: 100 million array ref=1359

Gordon Gekko
654
Gordon Gekko 2016.09.11 17:01  

Thank you guys for answers.

WHRoeder: yes, my measurement also says 5-6 times difference. Have you any idea for my original question?

whroeder1
13616
whroeder1 2016.09.12 14:41  
ggekko: WHRoeder: yes, my measurement also says 5-6 times difference. Have you any idea for my original question?
  1. Copy them in a loop.
  2. You don't state a why, so no other suggestions can be provided.
/
To add comments, please log in or register