I assume you want to max and average only over specific ranges in the multidimensional array. Using extension methods it’s a bit complicated, but you can do it like this:
var myArray1 = new double[320, 18];
var myArray2 = new double[8, 18];
int dim2 = myArray1.GetLength(1);
myArray2[0, 0] =
myArray1.Cast<double>().Select((val, idx) => new { idx, val }).Where(
x => x.idx % dim2 == 0 && x.idx / dim2 >= 11 && x.idx / dim2 < 18).Max(x => x.val);
myArray2[1, 0] =
myArray1.Cast<double>().Select((val, idx) => new { idx, val }).Where(
x => x.idx % dim2 == 0 && x.idx / dim2 >= 18 && x.idx / dim2 < 25).Average(x => x.val);
// ...
The downside is that you’ll be iterating over all elements all the time. So if this is performance critical, I rather suggest doing it the old fashioned way and in one pass:
myArray2[0, 0] = double.MinValue;
myArray2[1, 0] = 0;
for (int i = 0; i < myArray1.GetLength(0) + 1; i++) {
if (i >= 11 && i < 18 && myArray1[i, 0] > myArray2[0, 0]) myArray2[0, 0] = myArray1[i, 0];
if (i >= 18 && i < 25) myArray2[1, 0] += myArray1[i, 0];
if (i == 25) myArray2[1, 0] /= Math.Abs(25 - 18);
// ...
}
And a final suggestion: Do not put magic numbers into your code (11, 18, 25, …), but use consts for this. Later on nobody knows anymore what 25 actually means.
EDIT2: I got the extension method solution to work finally.
2
solved Average in Array C# [closed]