# Getting maximum of 6 arrays

I am trying to get the maximum of 6 arrays A, B, C, D, E, F.

Below is my code;

``````	maximum = Max(Max(A, B), Max(C, D) );
maximum = Max(Max(maximum, E), F);
``````

I am wondering if there are other more elegant ways to code this.

See here

Another (related) example for finding nth largest value per bar index

1 Like

@thankyou18 - can you please clarify what you mean by "maximum". An example of the output you expect/ want to achieve, will help us to help you.

Do you want the result to be a single number, or an array? The answer depends on how you intend to use the result, and you haven't given us that info.

Maximum returns the highest array among the 6 arrays. This is what the code does in the opening post.

So what is your problem now? Is there still anything unclear?
Either use the code lines of your post #1 or a flexible function such as the one of 1st link of post #2 being able to get max. of n-number of variables.

``````function VarGetMax( varname, num ) {
/// How to use:
local n, maxall;
maxall = -1e9;
for ( n = 1; n <= num; n++ )
maxall = Max( maxall, VarGet( varname + n ) );
return maxall;
}

// in place of A, B, C, D, E, F insert your arrays
var1 = A;
var2 = B;
var3 = C;
var4 = D;
var5 = E;
var6 = F;

// Overall maximum n variables
getmax = VarGetMax( "var", n = 6 );
printf( "\nMax. of %g Variables: %g", n, getmax );
``````

In order to make that function of the first link to work you just have to assign your A, B, ..., F arrays to consecutively numbered variable names having the same prefix before each number (prefix name does not matter). So in upper example it is "var" (again, example!).

And that prefix you have to insert as string to first argument of that function VarGetMax. And second argument being the number of variables to be iterated. So in your case n = 6.

Since variable "getmax" of upper code calculates maximum (per each bar index) of n-number of arrays it is type array itself too. If you want to get element(s) of that "getmax" array then apply usual methods described in AB manual (e.g. LastValue(), SelectedValue(), ..., looping).

Of course that function does not only work for arrays but for numbers also

``````//....

var1 = 11;
var2 = 0.2;
var3 = 3;
var4 = 400;
var5 = 50;
var6 = 6*6*6;

// Overall maximum n variables
getmax = VarGetMax( "var", n = 6 );
printf( "\nMax. of %g Variables: %g", n, getmax );
``````

If you want to get nth-highest/lowest per bar (which includes min/max) then use the matrix example of 2nd link.

Or whatever...

OK? Or is there still any problem?

5 Likes

Ok, @thankyou18, that's a bit clearer.

Sometimes people don't really know what they want, until they've seen the available options - company executives tend to be like that: They have a hunch about something, and want info to guide them, but they don't know the nitty-gritty, or how to specify what they want.

Some people (on this forum) make reference to and use scalars in their question, rather than arrays. It's only later that they reveal their intention to use arrays.

So, you want an array as the end-product, where each element of the resulting array contains the highest value of the 6 arrays at the same position in their respective arrays. For example:

``````for (loop = 0; loop < BarCount; loop++)
res[loop] = max(arr1[loop], arr2[loop], ..., arr6[loop]) ;
``````

Note: `max()` in the example above is only for illustrative purposes, and doesn't reflect how it works in AB.

I haven't looked at @fxshrat's code, above, but I'm sure there's lots in there to chew on.

1 Like

Hi fxshrat,