c# - 二维数组的第二个最小值
问题描述
我想获得二维数组的第二个最小值,我的原始数组包含很多零,我对此无能为力,但我想获得最小值,这就是我想到这个想法的原因,任何人都有线索?我尝试了以下顺序以获得有效的最低值。我只是把它放在啤酒上发布问题的代码,我不需要修改它我只想知道如何获得第二个最小值。
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]);
}
}
}
}
解决方案
你可以使用Linq
扩展方法很容易地得到你需要的东西。如您所知,您可以调用Cast<double>
将所有项目放入一个IEnumerable<double>
,所以现在您可以跟进Distinct
,它获取所有唯一数字,然后OrderBy(i => i)
对结果进行排序,最后您可以使用Skip
跳过第一个值然后FirstOrDefault
到之后得到第一个值(所以是倒数第二个数字):
double secondSmallestValue = twoDimensionalArrayOfValues
.Cast<double>()
.Distinct()
.OrderBy(i => i)
.Skip(1)
.FirstOrDefault();
如果您for
出于某种原因更喜欢循环方法,您可以通过跟踪最小值和次小值来完成类似的操作,然后遍历数组中的每个项目以查看是否找到比当前最小值更小的值。当你这样做时,只需设置secondSmallest = 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
被定义为一个10x10
填充了从0
到的随机整数的数组99
:
int rowCount = 10;
int colCount = 10;
Random rnd = new Random();
int[,] values = new int[rowCount, colCount];
for(int row = 0; row < rowCount; row++)
for (int col = 0; col < colCount; col++)
values[row, col] = rnd.Next(0, 100);
推荐阅读
- gemfire - 通过 GemfireTemplate.query 方法使用 HINT 和 TRACE
- apache - Apache2.4 需要指令配置
- reactjs - 出现错误:有条件地调用 React Hook “useSpring”
- unity3d - 使用纹理将模型导入 Unity(不加载纹理)
- java - @WebMvcTest 创建名为“securityConfiguration”的 bean 时出错
- javascript - 如何使用“multiple”道具获取 Vuetify Select 字段以与数据库中的 axios 数据进行交互?
- php - 第 15 集 laracasts.com 的问题 PHP 实践者
- html - 怎么去掉这句话
- assembly - 为什么 x86 通常不允许不是第一个源寄存器的目标寄存器?
- visual-studio-code - 允许自动缩进二维数组(如表格)的编辑器或扩展