二维数组的第二个最小值

时间:2019-05-03 21:38:44

标签: c# arrays

我想获取二维数组的第二个最小值,我原来的数组包含很多零,对此我无能为力,但我想获取最小值,这就是为什么我想到这个主意,有人知道吗?我尝试了以下顺序以获取最低价值。该代码我只是把它当作啤酒来发布问题,我不需要修改它,我只想知道如何获得第二个最小值。

  

low1 = result1.Cast()。Min();

        for (int m = 0; m < Weights.Count; m++)
        {
            int offset = m * ListCranelocations.Count;

            for (int i = 0; i < ListCranelocations.Count; i++)
            {
                for (int j = 0; j < ListPickLocations.Count; j++)
                {
                    double x = ListCranelocations[i].Lat - ListPickLocations[j].Lat;
                    double y = ListCranelocations[i].Lng - ListPickLocations[j].Lng;
                    R1[i] = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));

                    if ( R1[i] > Clearance )
                    {     
                        result1[i + offset, j] = Weights[m] * R1[i];

                         //Console.WriteLine(result1[i, j]);
                    } 

                }
            }
        }


        for (int m = 0; m < Weights.Count; m++)

        {
            int offset = m * ListCranelocations.Count;

            for (int i = 0; i < ListCranelocations.Count; i++)
            {
                for (int j = 0; j < ListSetlocations.Count; j++)
                {

                    double x = ListCranelocations[i].Lat - ListSetlocations[j].Lat;
                    double y = ListCranelocations[i].Lng - ListSetlocations[j].Lng;
                    R2[i] = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));

                    if (R2[i] > Clearance )
                    {

                        result2[i + offset, j] = Weights[m] * R2[i];

                        //  Console.WriteLine(result2[i, j]);
                    }


                }

            }
        }

        double low = 0;
        double low1 = 0;
        double low2 = 0;
        double low23 = 0;


        for (int i = 0; i < result1.GetLength(0); i++)
        {
            for (int j = 0; j < result1.GetLength(1); j++)
            {
                for (int k = 0; k < result2.GetLength(0); k++)
                {
                    for (int m = 0; m < result2.GetLength(1); m++)
                    {

                        if (!(result1[i, j] == 0) && !(result2[k, m] == 0))
                        {

                            result3[i, j] = result1[i, j] + "," + result2[k, m];


                            // Console.WriteLine(result3[i, j]);
                          /*  
                            if ((result1[i, j]) > (result2[k, m]))
                            {
                                highestMoment[i, j] = result1[i, j];
                            }
                            else
                            {
                                highestMoment[i, j] = result2[k, m];
                            }
                            */



                            low1 = result1.Cast<double>().Min();
                            low2 = result2.Cast<double>().Min();

                            if (low1 > low2)
                            {
                                low = low1;
                                Index[i, j] = "P";
                            }
                            else if (low1 > low2)
                            {
                                low = low2;
                                Index[i, j] = "S";
                            }

                            counter++;


                        }


                          // Console.WriteLine(highestMoment[i, j]);



                    }
                }

            }

        }

1 个答案:

答案 0 :(得分:2)

您可以使用Linq扩展方法轻松获得所需的内容。如您所知,您可以调用Cast<double>将所有项目放入IEnumerable<double>中,因此现在您可以跟进Distinct,后者获取所有唯一编号,然后{{1 }}对结果进行排序,最后您可以使用OrderBy(i => i)跳过第一个值,然后使用Skip获取之后的第一个值(所以倒数第二个):

FirstOrDefault

如果出于某种原因更喜欢使用double secondSmallestValue = twoDimensionalArrayOfValues .Cast<double>() .Distinct() .OrderBy(i => i) .Skip(1) .FirstOrDefault(); 循环方法,则可以通过跟踪最小和第二个最小的值,然后遍历数组中的每个项以查看是否找到一个,来完成类似的操作小于当前的最小值。完成后,只需设置forsecondSmallest = smallest

smallest = currentValue

在上面的代码中,var smallestValue = int.MaxValue; var secondSmallestValue = int.MaxValue; for(int row = 0; row < values.GetUpperBound(0); row++) { for (int col = 0; col < values.GetUpperBound(1); col++) { var thisValue = values[row, col]; if (thisValue < smallestValue) { // Here you have row and col variables if you need to // keep track of the indexes at which the items were found secondSmallestValue = smallestValue; smallestValue = thisValue; } else if (thisValue < secondSmallestValue) { secondSmallestValue = thisValue; } } } 被定义为values数组,其中填充了从10x100的随机整数:

99