统一平方距离矩阵c#

统一平方距离矩阵c#,c#,matrix,C#,Matrix,我需要把两个不同的方阵统一成一个 我们需要缓存一个非常大的距离矩阵 我们使用的是一个地图提供商,它对每个调用收费,因此我们需要进行许多小的调用,然后将矩阵统一到一个矩阵中。 对于未计算的矩阵单元,我们可以插入一个非常大的数字,比如inf 因此,让我详细说明一下(为了简单起见,假设我们有两个矩阵): 我需要创建以下矩阵 a b c d e f g c = a [0 5 2 6 inf inf inf] b [5 0 7 3

我需要把两个不同的方阵统一成一个

我们需要缓存一个非常大的距离矩阵

我们使用的是一个地图提供商,它对每个调用收费,因此我们需要进行许多小的调用,然后将矩阵统一到一个矩阵中。 对于未计算的矩阵单元,我们可以插入一个非常大的数字,比如inf

因此,让我详细说明一下(为了简单起见,假设我们有两个矩阵):

我需要创建以下矩阵

       a   b   c   d   e   f   g
c = a [0   5   2   6  inf inf inf]
    b [5   0   7   3  inf inf inf]
    c [2   7   0   9  inf inf inf]
    d [6   3   9   0  inf inf inf]
    e [inf inf inf inf 0   5   8 ]
    f [inf inf inf inf 5   0   18]
    g [inf inf inf inf 8   18   0]
是否有一个统一此类矩阵的库,或者我应该从头开始构建它


多谢各位

合并2个阵列的最简单内置选项是


如果您有充分的理由在生成的矩阵中使用“inf”,则更有可能需要手动扩展初始数组,例如使用
int.MaxValue

我已经编写了一段代码,可以满足我的要求。通过在两个不同大小的矩阵的连接上添加一个较大的值,从两个不同大小的矩阵创建一个欧几里德平方矩阵

Dictionary<string, Dictionary<string, double>> firstMatrix
                = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, double>>>(originalMatrixText);
Dictionary<string, Dictionary<string, double>> secondMatrix
                = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, double>>>(secondaryMatrixText);

//Create the dimension of the full matrix
List<string> totalPoints = new List<string>();
totalPoints.AddRange(firstMatrix.Keys);
totalPoints.AddRange(secondMatrix.Keys);

Dictionary<string, Dictionary<string, double>> finalMatrix = new Dictionary<string, Dictionary<string, doube>>();

foreach (string pointA in totalPoints)
{
    string keyRow = pointA;
    Dictionary<string, double> cells = new Dictionary<string, double>();

    if (!finalMatrix.ContainsKey(keyRow))
    {
        foreach (string pointB in totalPoints)
        {
            string keyCell = pointB;
            double distance = 0;
            if (!cells.ContainsKey(keyCell))
            {
                if (firstMatrix.ContainsKey(keyRow) && firstMatrix.ContainsKey(keyCell))
                {
                    //If the element was in the first matrix use this value
                    distance = firstMatrix[keyRow][keyCell];
                }
                else if (secondMatrix.ContainsKey(keyRow) && secondMatrix.ContainsKey(keyCell))
                {
                    //If the element was in the second matrix use this value
                    distance = secondMatrix[keyRow][keyCell];
                }
                else
                {
                    //If the element did not exist in either matrix use the max Value of Double
                    distance = double.MaxValue;
                }
                cells.Add(keyCell, distance);
            }
        }
        finalMatrix.Add(keyRow, cells);
    }
}
字典矩阵
=JsonConvert.DeserializeObject(originalMatrix);
字典二次矩阵
=JsonConvert.DeserializeObject(secondaryMatrixText);
//创建完整矩阵的维度
List totalPoints=新列表();
totalPoints.AddRange(firstMatrix.Keys);
totalPoints.AddRange(secondMatrix.Keys);
Dictionary finalMatrix=新字典();
foreach(totalPoints中的字符串pointA)
{
字符串keyRow=pointA;
字典单元格=新字典();
如果(!finalMatrix.ContainsKey(keyRow))
{
foreach(totalPoints中的字符串pointB)
{
字符串keyCell=pointB;
双倍距离=0;
如果(!cells.ContainsKey(keyCell))
{
if(firstMatrix.ContainsKey(keyRow)和&firstMatrix.ContainsKey(keyCell))
{
//如果元素在第一个矩阵中,则使用此值
距离=第一矩阵[keyRow][keyCell];
}
else if(secondMatrix.ContainsKey(keyRow)和&secondMatrix.ContainsKey(keyCell))
{
//如果元素在第二个矩阵中,则使用此值
距离=第二矩阵[keyRow][keyCell];
}
其他的
{
//如果两个矩阵中均不存在该元素,则使用Double的最大值
距离=double.MaxValue;
}
单元格。添加(关键单元格,距离);
}
}
添加(关键行,单元格);
}
}

为什么在生成的数组中需要这些“inf”?我不需要。还不如什么都可以。我需要它是一个大数字,以“防止”一些链接,因为它的路由应用程序。你好鲍里斯。我需要矩阵是欧几里得平方矩阵,这就是为什么我必须附加额外产生的值。这不会产生要求的输出。
double[][] a = {
    new double[] { -1, -2, -1, -2 },
    new double[] { -5, -3, -4, -2 },
    new double[] { -1, -2, -1, -2 },
    new double[] { -5, -3, -4, -2 }
};

double[][] b = {
    new double[] {1, 1, -1 },
    new double[] { 3, 2, 3 },
    new double[] { -5, -3, -4}
};

double[][] result = a.Concat(b).ToArray();

foreach (var line in result)
{
    Console.WriteLine(string.Join(',', line));
}
Dictionary<string, Dictionary<string, double>> firstMatrix
                = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, double>>>(originalMatrixText);
Dictionary<string, Dictionary<string, double>> secondMatrix
                = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, double>>>(secondaryMatrixText);

//Create the dimension of the full matrix
List<string> totalPoints = new List<string>();
totalPoints.AddRange(firstMatrix.Keys);
totalPoints.AddRange(secondMatrix.Keys);

Dictionary<string, Dictionary<string, double>> finalMatrix = new Dictionary<string, Dictionary<string, doube>>();

foreach (string pointA in totalPoints)
{
    string keyRow = pointA;
    Dictionary<string, double> cells = new Dictionary<string, double>();

    if (!finalMatrix.ContainsKey(keyRow))
    {
        foreach (string pointB in totalPoints)
        {
            string keyCell = pointB;
            double distance = 0;
            if (!cells.ContainsKey(keyCell))
            {
                if (firstMatrix.ContainsKey(keyRow) && firstMatrix.ContainsKey(keyCell))
                {
                    //If the element was in the first matrix use this value
                    distance = firstMatrix[keyRow][keyCell];
                }
                else if (secondMatrix.ContainsKey(keyRow) && secondMatrix.ContainsKey(keyCell))
                {
                    //If the element was in the second matrix use this value
                    distance = secondMatrix[keyRow][keyCell];
                }
                else
                {
                    //If the element did not exist in either matrix use the max Value of Double
                    distance = double.MaxValue;
                }
                cells.Add(keyCell, distance);
            }
        }
        finalMatrix.Add(keyRow, cells);
    }
}