Find out minimum value from a multidimensional array with NaNs

…衆ロ難τιáo~ 提交于 2020-01-04 04:04:21

问题


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!


回答1:


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));



回答2:


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.




回答3:


public static double MinIsNumber(double[,] array)
{
    return array.Cast<double>()
        .Where(n => !double.IsNaN(n))
        .Min();
}


来源:https://stackoverflow.com/questions/15395006/find-out-minimum-value-from-a-multidimensional-array-with-nans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!