如何在 C# 中使用 3 个数组分配变量
How to assign a variable using 3 arrays in C#
我正在尝试为有限元地质学软件编写插件。
我有三个数组,它们本质上是一个坐标系。我想根据变量在网格系统中的位置为它赋值。基本上我想说如果我的节点在一个x范围和一个y范围内,那么我在这个节点的含水层厚度就是这个值。到目前为止我有这个。
//create an array of xcoords of data points:
double[] xcoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
//create an array of ycoords of data points:
double[] ycoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
//create an array of aquifer thickness
double[] aquiferThicknessPoints = new double[121]
{
10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13,
10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13,
12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14,
13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15,
14, 15, 17, 18, 21, 17, 18, 18, 19, 17, 16,
15, 15, 17, 17, 20, 21, 21, 19, 19, 18, 18,
15, 15, 17, 20, 20, 21, 22, 21, 19, 19, 19,
16, 17, 19, 20, 22, 23, 22, 21, 20, 20, 20,
17, 18, 20, 22, 23, 24, 24, 23, 22, 20, 21,
18, 19, 21, 22, 24, 25, 24, 23, 22, 22, 22,
19, 19, 22, 22, 24, 25, 25, 23, 23, 22, 23,
};
dataPointSpacingHalf = dataPointSpacing / 2;
for (int i = 0; i < xcoord.Length; i++)
{
for (int j = 0; j < ycoord.Length; j++)
{
if (nodeX >= (xcoord[i] - dataPointSpacingHalf) && (nodeX < (xcoord[i] + dataPointSpacingHalf)) && (nodeY >= (ycoord[j] - dataPointSpacingHalf) && (nodeY < (ycoord[j] + dataPointSpacingHalf))))
{
aquiferThickness = aquiferThicknessPoints[?];
}
}
}
我可以看到嵌套的 for 循环将如何循环 110 次,但我不知道如何将阵列中的含水层厚度分配给每个循环。
我对解决这个问题的任何方法都持开放态度,因为我是编程的新手,但我仍然不确定哪种方法是实现目标的最佳方法。
您想为您的 aquiferThicknessPoints
使用二维数组:
double[,] aquiferThicknessPoints = new double[,]
{
{10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13},
{10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13},
{12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14},
{13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15},
// the rest
};
然后您可以使用两个坐标寻址数据:
aquiferThickness = aquiferThicknessPoints[j, i];
(或i, j
,您的数据是如何组织的并不明显)
只需使用 i * xcoord.Length + j
而不是 ?
代码如下:
for (int i = 0; i < xcoord.Length; i++)
{
for (int j = 0; j < ycoord.Length; j++)
{
//Here is the magic!
//without considering coordinates
//aquiferThickness[i, j] = aquiferThicknessPoints[i * xcoord.Length + j];
//considering coordinates
aquiferThickness[i, j] =
aquiferThicknessPoints[
CoordToIndex(xNode,indexedCoords) * xcoord.Length +
CoordToIndex(yNode,indexedCoords)];
}
}
还要考虑xNode,Node坐标,可以采用这种方式
Dictionary<int, double> indexedCoords = new Dictionary<int, double> { { 0, 0 }, { 1, 5 }, { 2, 10 }, .... };
int CoordToIndex(double node, Dictionary<int, double> indexedCoords)
{
return indexedCoords.First(i => i.Value > node).Key;
}
我正在尝试为有限元地质学软件编写插件。
我有三个数组,它们本质上是一个坐标系。我想根据变量在网格系统中的位置为它赋值。基本上我想说如果我的节点在一个x范围和一个y范围内,那么我在这个节点的含水层厚度就是这个值。到目前为止我有这个。
//create an array of xcoords of data points:
double[] xcoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
//create an array of ycoords of data points:
double[] ycoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
//create an array of aquifer thickness
double[] aquiferThicknessPoints = new double[121]
{
10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13,
10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13,
12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14,
13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15,
14, 15, 17, 18, 21, 17, 18, 18, 19, 17, 16,
15, 15, 17, 17, 20, 21, 21, 19, 19, 18, 18,
15, 15, 17, 20, 20, 21, 22, 21, 19, 19, 19,
16, 17, 19, 20, 22, 23, 22, 21, 20, 20, 20,
17, 18, 20, 22, 23, 24, 24, 23, 22, 20, 21,
18, 19, 21, 22, 24, 25, 24, 23, 22, 22, 22,
19, 19, 22, 22, 24, 25, 25, 23, 23, 22, 23,
};
dataPointSpacingHalf = dataPointSpacing / 2;
for (int i = 0; i < xcoord.Length; i++)
{
for (int j = 0; j < ycoord.Length; j++)
{
if (nodeX >= (xcoord[i] - dataPointSpacingHalf) && (nodeX < (xcoord[i] + dataPointSpacingHalf)) && (nodeY >= (ycoord[j] - dataPointSpacingHalf) && (nodeY < (ycoord[j] + dataPointSpacingHalf))))
{
aquiferThickness = aquiferThicknessPoints[?];
}
}
}
我可以看到嵌套的 for 循环将如何循环 110 次,但我不知道如何将阵列中的含水层厚度分配给每个循环。
我对解决这个问题的任何方法都持开放态度,因为我是编程的新手,但我仍然不确定哪种方法是实现目标的最佳方法。
您想为您的 aquiferThicknessPoints
使用二维数组:
double[,] aquiferThicknessPoints = new double[,]
{
{10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13},
{10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13},
{12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14},
{13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15},
// the rest
};
然后您可以使用两个坐标寻址数据:
aquiferThickness = aquiferThicknessPoints[j, i];
(或i, j
,您的数据是如何组织的并不明显)
只需使用 i * xcoord.Length + j
而不是 ?
代码如下:
for (int i = 0; i < xcoord.Length; i++)
{
for (int j = 0; j < ycoord.Length; j++)
{
//Here is the magic!
//without considering coordinates
//aquiferThickness[i, j] = aquiferThicknessPoints[i * xcoord.Length + j];
//considering coordinates
aquiferThickness[i, j] =
aquiferThicknessPoints[
CoordToIndex(xNode,indexedCoords) * xcoord.Length +
CoordToIndex(yNode,indexedCoords)];
}
}
还要考虑xNode,Node坐标,可以采用这种方式
Dictionary<int, double> indexedCoords = new Dictionary<int, double> { { 0, 0 }, { 1, 5 }, { 2, 10 }, .... };
int CoordToIndex(double node, Dictionary<int, double> indexedCoords)
{
return indexedCoords.First(i => i.Value > node).Key;
}