【问题标题】:How to assign a variable using 3 arrays in C#如何在 C# 中使用 3 个数组分配变量
【发布时间】: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


【解决方案1】:

只需使用i * xcoord.Length + j insted of ?

代码如下:

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

【讨论】:

  • @Houssein Narimani Rad。是的, [i * xcoord.Length + j] 完美的把戏。我知道这很简单。非常感谢您的帮助。
  • @GeorgeFrench 很高兴为您提供帮助
【解决方案2】:

您想为您的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,你的数据是如何组织的并不明显)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 2020-09-16
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多