I have a bidimensional array ( double[ , ]
), and I want to find out what's the minimum. I tried Linq.Select.Min, but since my arrays typically contain NaN
values, then minvalue
is always NaN
.
So, I need some way to find the Minimum value that "skips" the NaNs.
Any help is much appreciated!
Today is the day for extension methods! Use this to have a generic Min()
function on all your double[,]
!
Heres some generic [,] extensions. Please note this will only be available for types that implement IComparable
This one ignores nothing:
public static T Min<T>(this T[,] arr) where T : IComparable
{
bool minSet = false;
T min = default(T);
for (int i = 0; i < arr.GetLength(0); i++)
for (int j = 0; j < arr.GetLength(1); j++)
if (!minSet)
{
minSet = true;
min = arr[i, j];
}
else if (arr[i, j].CompareTo(min) < 0)
min = arr[i, j];
return min;
}
This one will let you specify a value to ignore, and in the special case where the array only contains the ignored value, it will return the ignored value.
public static T Min<T>(this T[,] arr, T ignore) where T : IComparable
{
bool minSet = false;
T min = default(T);
for (int i = 0; i < arr.GetLength(0); i++)
for (int j = 0; j < arr.GetLength(1); j++)
if (arr[i, j].CompareTo(ignore) != 0)
if (!minSet)
{
minSet = true;
min = arr[i, j];
}
else if (arr[i, j].CompareTo(min) < 0)
min = arr[i, j];
return (minSet) ? min : ignore;
}
The output of the following code is
NaN
-10
double[,] d = new double[5, 5]
{
{ 0, 1, 2, 3, 4 },
{ 5, 6, 7, 8, 9 },
{ 10, 11, -10, 12, 13 },
{ 14, 15, 16, 17, 18 },
{ 19, double.NaN, 21, 22, 23 }
};
Console.WriteLine(d.Min());
Console.WriteLine(d.Min(double.NaN));
Try this:
var minvalue = System.Linq.Enumerable.Range(0, 4)
.Where(i => !Double.IsNa(myarray[i,1]))
.Select(i => myarray[i, 1]).Min();
Update:
double[,] myarray = new double[4,4];
var rows = myarray.GetLength(0);
var cols = myarray.GetLength(1);
Enumerable.Range(0, rows * cols)
.Where(i => !Double.IsNaN(myarray[i/rows, i%rows]))
.Select(i => myarray[i/rows, i%rows])
.Min();
Fancier use of .Range() is undoubtedly possible, which I will now research.
public static double MinIsNumber(double[,] array)
{
return array.Cast<double>()
.Where(n => !double.IsNaN(n))
.Min();
}