将数组复制到较小的数组

时间:2014-11-01 20:46:55

标签: c# arrays

尝试将数组复制到数组时出现以下问题。

假设我的起始数组是15x11。

我想要的数组是12x8。

我希望它能像这样工作:

如果第一个数组大于第二个数组,则跳过每隔一行/每列直到它具有适当的大小。

以我的例子:

第一排,跳过第二排,第三排,第四顺位,第五顺位,第五顺位,第六顺位,第六排,现在适合其余部分。

列相同。

到目前为止,我试图将其包装成代码但没有成功。有人可以帮忙吗?

谢谢。

编辑:这是我尝试过的列,例如

for (int i = 0; i < n.GetLength(0); i++)
        {
            for (int j = 0; j < n.GetLength(1); j++)
            {
                while (remcol > 0)
                {
                    n[i, j] = g[i, remcol - (remcol - k)];
                    k++;
                    remcol--;
                }
                n[i, j] = g[i, j + k];
            }
        }

K在开始时为0,而remcol是g.GetLenght(1) - n.GetLenght(1)。 Gis更大的数组,n更小。

2 个答案:

答案 0 :(得分:0)

int rowDif = g.GetLength(0) - n.GetLength(0);
int colDif = g.GetLength(1) - n.GetLength(1);

for (int i = 0; i < n.GetLength(0); i++)  
{
  int mappedRow = i * 2 <= 2 * (rowDif - 1) ? 2 * i : 2 * rowDif + ( i - rowDif + 1);
  for (int j = 0; j < n.GetLength(1); j++)
  {    
    int mappedCol = j * 2 <= 2 * (colDif - 1) ? 2 * j : 2 * colDif + ( j - colDif + 1);
    n[i, j] = g[mappedRow, mappedCol];
  }
}

答案 1 :(得分:0)

这不能完成这项工作吗?它复制尺寸为n / 2 <= g <= n * 2的阵列。附测试案例。它使用标准循环和if语句以便于阅读。

void CopydArrayScaled(int[,] g, int[,] n)
{
    int gRowSkip = g.GetLength(0) - n.GetLength(0);
    for (int nRowIdx = 0, gRowIdx = 0; nRowIdx < n.GetLength(0); nRowIdx++)
    {
        int gColSkip = g.GetLength(1) - n.GetLength(1);
        for (int nColIdx = 0, gColIdx = 0; nColIdx < n.GetLength(1); nColIdx++)
        {
            n[nRowIdx, nColIdx] = g[gRowIdx, gColIdx];
            if (gColSkip > 0)
            {
                gColSkip--;
                gColIdx += 2;
            }
            else if (gColSkip < 0)
            {
                if (nColIdx % 2 == 1)
                {
                    gColIdx++;
                    gColSkip++;
                }
            }
            else
            {
                gColIdx++;
            }
        }
        if (gRowSkip > 0)
        {
            gRowSkip--;
            gRowIdx += 2;
        }
        else if (gRowSkip < 0)
        {
            if (nRowIdx % 2 == 1)
            {
                gRowIdx++;
                gRowSkip++;
            }
        }
        else
        {
            gRowIdx++;
        }
    }
}

void CopydArrayScaled2(int[,] g, int[,] n)
{
    int gRowSkip = g.GetLength(0) - n.GetLength(0);

    for (int nRowIdx = 0; nRowIdx < n.GetLength(0); nRowIdx++)
    {
        int gRowIdx = nRowIdx * g.GetLength(0) / n.GetLength(0);
        int gColSkip = g.GetLength(1) - n.GetLength(1);
        for (int nColIdx = 0; nColIdx < n.GetLength(1); nColIdx++)
        {
            int gColIdx = nColIdx * g.GetLength(1) / n.GetLength(1);
            n[nRowIdx, nColIdx] = g[gRowIdx, gColIdx];
        }
    }
}

void PrintArray(int[,] a)
{
    Console.WriteLine("Destination dimensions: [{0},{1}]", a.GetLength(0), a.GetLength(1));
    for (int i = 0; i < a.GetLength(0); i++)
    {
        for (int j = 0; j < a.GetLength(1); j++)
        {
            if (j != 0) Console.Write(",");
            Console.Write(a[i, j]);
        }
        Console.WriteLine();
    }
}

void Test()
{
    int[,] g = new int[4, 4] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } };

    int[,] n = new int[2, 2];
    CopydArrayScaled(g, n);
    PrintArray(n);

    int [,] n2 = new int[8, 8];
    CopydArrayScaled(g, n2);
    PrintArray(n2);

    int[,] n3 = new int[4, 4];
    CopydArrayScaled(g, n3);
    PrintArray(n3);
}

输出:

Destination dimensions: [2,2]
0,2
8,10
Destination dimensions: [8,8]
0,0,1,1,2,2,3,3
0,0,1,1,2,2,3,3
4,4,5,5,6,6,7,7
4,4,5,5,6,6,7,7
8,8,9,9,10,10,11,11
8,8,9,9,10,10,11,11
12,12,13,13,14,14,15,15
12,12,13,13,14,14,15,15
Destination dimensions: [4,4]
0,1,2,3
4,5,6,7
8,9,10,11
12,13,14,15

注意:添加了版本2.它允许n和g为任意大小,但不符合原始问题:跳过每个第二行/列。对于尺寸(n * 2> g),skip方法给出了奇怪的结果。

相关问题