2014-11-01 37 views
0

嘗試將陣列複製到陣列時出現以下問題。將陣列複製到更小的陣列

說我的起始數組是15x11。我想要的數組是12x8。

我希望它像這樣工作:

如果第一陣列比第二陣列更大,跳過每秒行/列,直到它有適當的大小。

所以我的例子:

拿第一排,跳過2,以第3,第4跳,走5日,6日跳,拿剩下的,因爲它現在適合。

相同的列。

我試圖把它包裝到代碼中,但迄今爲止沒有成功。誰能幫忙?

謝謝。

編輯:這是我試過的列例如

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越小。

+0

你嘗試過什麼嗎? – 2014-11-01 20:48:51

+1

告訴我們你試過的是什麼 – dotnetom 2014-11-01 20:50:38

+0

我嘗試着聲明2個變量,它們是biggersize-smallersize,然後以某種方式將它變成2 for循環(它遍歷小數組的索引)。然後,我嘗試在var> 0的情況下沿着線條擺弄smth,例如對於列,smallerarray [i,j] =更大的數組[i,j + 1],但是vs告訴我要麼檢查索引,要麼放入索引錯誤的價值觀。在我的幫助變量達到0後,我可能遇到了編碼零件的問題。 – Ironowicz 2014-11-01 20:53:42

回答

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

乾杯,我修改了一下,它的工作原理。謝謝你的幫助 :) – Ironowicz 2014-11-02 14:13:58

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),跳過方法會產生奇怪的結果。