gpt4 book ai didi

c# - 如何在 C# 中使用 3 个数组分配变量

转载 作者:行者123 更新时间:2023-11-30 16:53:16 24 4
gpt4 key购买 nike

我正在尝试为有限元地质学软件编写插件。

我有三个数组,它们本质上是一个坐标系。我想根据变量在网格系统中的位置为它赋值。基本上我想说如果我的节点在一个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 次,但我不知道如何将数组中的含水层厚度分配给每个循环。

我对解决这个问题的任何方法都持开放态度,因为我是编程的新手,仍然不确定哪种方法是实现目标的最佳方法。

最佳答案

只需使用 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、yNode坐标,可以采用这种方式

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;
}

关于c# - 如何在 C# 中使用 3 个数组分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com