c# - 二维数组的最大值最小值
问题描述
好的,我还有一个问题,因为正如我告诉你的那样,我试图将所有这些值放入结果 3 中,就像我之前提到的那样( x , y ),然后我必须得到每个点的最大值( x , y )对于整个点然后获得最大值的最小值,我设法获得最大值但是当我尝试获得它们中的最小值时,它显示为零。
这是代码。
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++)
{
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];
}
Console.WriteLine(highestMoment[i, j]);
if (lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
这是整个代码
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0), result1.GetLength(1)];
double[,] highestMoment = new double[result3.GetLength(0), result3.GetLength(1)];
double[,] lowestMoment = new double[highestMoment.GetLength(0), highestMoment.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
//Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i +offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
//Console.WriteLine(result2[i, j]);
}
}
}
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++)
{
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];
}
Console.WriteLine(highestMoment[i, j]);
if (lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
解决方案
在将lowestMoment
其与highestMoment
. 由于 a 的默认值为double
0,因此最低时刻将始终低于您将其与结果 0 进行比较的任何值。
这可能是您正在寻找的:
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
if (lowestMoment[i, j] == 0
|| lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(highestMoment[i, j]);
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
推荐阅读
- html - 以两种方式更新选中的单选按钮
- python - ValueError:无法分配“'x'”:“y”必须是“z”实例
- css - 如何在反应中使用两个api url
- javascript - require('fs') 不起作用,不知道为什么控制台说有错误
- c++ - C++[错误] 不能传递非平凡可复制类型的对象'std::string {aka class std::basic_string
}' 通过 '...' - tensorflow - 如何在多个没有 GPU 的设备上训练 TensorFlow?
- javascript - React Material UI - Snackbar 在对话框关闭时关闭,但它不应该关闭
- flutter - 颤振/飞镖:如何在飞镖 FFI 中使用异步回调?
- html - 我想使用 findElement(By.className()) 打印“Apple”和“tomato”
- android - 如何使用谷歌地图 Api 获得最精确的位置