【发布时间】:2015-10-20 08:29:32
【问题描述】:
我正在尝试为有限元地质学软件编写插件。
我有三个数组,它们本质上是一个坐标系。我想根据变量在网格系统中的位置为变量赋值。基本上我想说如果我的节点在 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 次,但我不知道如何将我的含水层厚度从我的数组分配给每个循环。
我愿意接受任何解决此问题的方法,因为我是编程新手,但仍然不确定哪种方法是实现目标的最佳方法。
【问题讨论】:
-
你能提供更多数据吗? nodeX 和 aquiferThickness 有什么作用?
-
我们无法直接回答这个问题,因为我们不知道您的
x和y坐标与您的double[110]的关系(为什么不是double[121]顺便提一句?)。您应该考虑为您的厚度使用二维数组,而不是一维数组,这样您就可以通过aquiferThickness[x][y]访问它,否则,您的访问可能类似于aquiferThickness[10 * x + y] = ...。 -
是的,nodeX 和 nodeY 值是从软件传递的,由网格属性定义,因此可以是任何 x 或 y 值。 aquiferThickness 可以是我选择的任何值。
-
您需要最近已知点的厚度还是定义网格坐标之间的插值? (请将此信息添加到您的问题中)
-
我同意@LInsoDeTeh。最好的解决方案很可能是完全重写您尝试执行此操作的方式,但是您没有提供几乎足够的信息让我们完成此操作。不幸的是,如果您这样做了,那么该问题将超出该网站的范围。
标签: c# arrays for-loop while-loop nested-loops