max value of 2D array

 

Hi,

I have a 2d-array and want to get the max value of each column.

The ArrayMaximum()-Function is for 1d-arrays only.

In the picture you can see an example. The numbers are stored in an 2d-array. In TF5 the highest number is 71. Is there any function in mql4 to get to this value?

I've looked over this forum and on google.... no solution yet. But I cannot believe that I am the only one with this topic.


Does anybody know?

A workaround would be to built up 4 different 1d-arrays.

Files:
array.jpg  17 kb
 

A workaround would be to built up 4 different 1d-arrays.

or you could right your own little function, its just a few lines of code.

 
Florian Riedrich:

Please post in the correct section in future.

I will move this to the MQL4 and Metatrader 4 section.

 
Paul Anscombe:

or you could right your own little function, its just a few lines of code.

Do you have an example for me to guide me?

 
Florian Riedrich:

Do you have an example for me to guide me?

sure give me a few minutes...

 
Florian Riedrich:

Do you have an example for me to guide me?

to be clear do you want the maximum of the whole array or the maximum of the second dimension for each of the first dimensions?

 
Paul Anscombe:

to be clear do you want the maximum of the whole array or the maximum of the second dimension for each of the first dimensions?

Thanks for helping!

I want the maximum of the second dimension for each of the first dimensions - not the whole array

 
Just typed; not compiled, not tested.
template<typename T>
T ArrayMaximum2D(const T& array[][2], int iCol=1, int iEnd=WHOLE_ARRAY, iBeg=0){
   if(iEnd == WHOLE_ARRAY) iEnd = ArrayRange(array, 0);
   T max=array[iBeg][iCol];
   while(--iEnd > iBeg){
      T value=array[iEnd][iCol];
      if(max < value) max = value;
   }
   return max;
}
////////////////
int a[][2]={ 1,2,3,4,5,6 }
a[1][2]             //4
ArrayMaximum2d(a,1) //5
ArrayMaximum2d(a,2) //6
Just typed; not compiled, not tested.
 
Florian Riedrich:

Thanks for helping!

I want the maximum of the second dimension for each of the first dimensions - not the whole array

// SCRIPT

int   iMyArray[10][5];

void OnStart()
{
   // seed some values for testing
   ArrayInitialize(iMyArray,1);
   iMyArray[0][3] = 7;
   iMyArray[1][4] = 8;
   iMyArray[2][1] = 9;
   iMyArray[3][4] = 88;
   iMyArray[4][2] = 75;
   iMyArray[5][3] = 7;
   iMyArray[6][1] = 6;
   iMyArray[7][0] = 19;
   iMyArray[8][2] = 17;
   iMyArray[9][3] = 12;
   
   int iLargest[10];    // array to store largest 2nd number for each 1st dimension 
   ArrayInitialize(iLargest,INT_MIN); // seed to minimum   

   for(int iFirst=0; iFirst<10; iFirst++)   // loop through first dimensions
   {
      for(int iSecond=0; iSecond<5; iSecond++)  // loop through 2nd dimension
         iLargest[iFirst] = MathMax(iMyArray[iFirst][iSecond], iLargest[iFirst]);

      // print results
      Print("1st dimension " + iFirst + " largest value " + iLargest[iFirst]); 
   }
}

here you go then...

 
Thanks William and Paul. I will look through your code and try to adopt to my code. :-)
Reason: