首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数组复制到较小的数组

将数组复制到较小的数组
EN

Stack Overflow用户
提问于 2014-11-01 20:46:54
回答 2查看 117关注 0票数 0

当试图将数组复制到数组中时,我遇到了以下问题。

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

我想要的数组是12x8。

我想让它像这样运作:

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

因此,以我的例子:

跳第一排,跳第二,跳第三,跳第4,跳第5,跳第6,其余的都按现在的情况来做。

纵队也一样。

我试图把它打包成代码,但到目前为止没有成功。有人能帮忙吗?

谢谢。

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

代码语言:javascript
复制
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,g.GetLenght(1) -n.GetLenght为(1)。Gis较大的阵列,n较小。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-01 21:20:31

代码语言:javascript
复制
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];
  }
}
票数 0
EN

Stack Overflow用户

发布于 2014-11-01 22:51:05

这样不就行了吗?它复制维为n/2的数组( <= g <= n*2 )。附加测试用例。它使用标准循环和简单的if语句来实现可读性。

代码语言:javascript
复制
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);
}

产出:

代码语言:javascript
复制
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),跳过方法给出了奇怪的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26693066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档