2014-11-01 61 views
2

我有一個數組宣告像這樣:修剪INT的2D陣列從不需要的值

int[,] binaryzacja = new int[bmp2.Width, bmp2.Height]; 

在那裏的尺寸是一個圖象的尺寸(通常這會是200×200)。黑色像素由1表示的所有其它像素由0

代表比方說,我的陣列看起來像這樣:

0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 1 0 1 1 1 0 
0 1 1 1 0 0 0 
0 0 1 1 0 0 0 
0 0 0 0 0 0 0 

我想刪除只包含0的所有行列。所以,如果我的例子數組是7列,6行;如果我的例子數組是7列,6行;如果我的例子數組是7列,6行;我想要一個新的陣列,這將是5列,3行看起來像這樣:

1 0 1 1 1 
1 1 1 0 0 
0 1 1 0 0 
+0

你給你的代碼的粗線條聽起來像它會工作。爲什麼不向我們展示代碼,以便我們可以看到可能出現的問題? – user12864 2014-11-01 17:58:56

+1

如何填充陣列?爲什麼不在插入時記錄每行和每列。爲每個列布爾,只要你做一個插入(不是0)將其設置爲true。相同的行... – Pleun 2014-11-01 18:53:36

+0

我只是建議你添加每行或列的元素,如果0跳過它。這是一個算法問題。 – 2014-11-01 21:00:50

回答

3

您可以將您的[,]List

var list = new List<List<int>>(); 

for (var i = 0; i < binary.GetLength(0); i++) 
{ 
    var row = new List<int>(); 
    for(var j = 0; j < binary.GetLength(1); j++) 
     row.Add(binary[i, j]); 
    list.Add(row); 
} 

然後刪除它們所有的行和列零:

// Remove the rows 
list = list.Where(row => row.Contains(1)).ToList(); 
// Reverse the matrix and apply the same procedure to remove the columns 
list = Transpose(list); 
list = list.Where(row => row.Contains(1)).ToList(); 
// Get back the original order of the rows and columns 
list = Transpose(list); 

static List<List<int>> Transpose(List<List<int>> input) 
{ 
    return input.ElementAt(0).Select((item, index) => 
     { 
      var row = new List<int>{input[0][index]}; 
      input.ForEach(el => row.Add(el.ElementAt(index))); 
      return row; 
      }).ToList(); 
     } 
} 

然後將結果列表轉換回[,]

int[,] binaryResult = new int[list.Count(), list.First().Count()]; 

for (int i = 0; i < binaryResult.GetLength(0); i++) 
    for (int j = 0; j < binaryResult.GetLength(1); j++) 
     binaryResult[i, j] = list.ElementAt(i).ElementAt(j); 

當然,您應該將這些解壓縮到方法中。

+0

列怎麼樣?除此之外,這是如此令人難以置信的低效率。LINQ時代的年輕程序員是否根本想到內存使用?創建如此多的臨時數據,列表,查詢...... – 2014-11-01 19:08:04

+1

@KonradKokosa,不,玩具項目被扔掉,列表中有幾十個元素的上衣,爲什麼會想到記憶?你會不會考慮讓你的代碼更乾淨(應該是)而不是更快? – mihai 2014-11-01 19:12:05

+0

學習玩具項目是最好的機會。但仍然你的解決方案只刪除行,所以我很驚訝關於upvotes ... – 2014-11-01 22:01:54

0

循環遍歷行,並跟蹤某些數組中的空行。 對列執行相同操作。

使用此信息,創建一個適當行和列的新數組。 用雙循環循環。如果我等於數組中的一行,或者j等於列中的行,則跳過該值。

1

沒有花哨的東西:

int width = binaryzacja.GetLength(0); 
int height = binaryzacja.GetLength(1); 

int newWidth = width; 
int newHeight = height; 

int x, y, x2, y2; 

for (y = 0; y < height; y++) 
{ 
    if (IsRowEmpty(binaryzacja, y)) newHeight--; 
} 

for (x = 0; x < width; x++) 
{ 
    if (IsColumnEmpty(binaryzacja, x)) newWidth--; 
} 

int[,] binaryzacja2 = new int[newWidth, newHeight]; 


// copy to new array 
for (y2 = y = 0; y < height; y++) 
{ 
    if (!IsRowEmpty(binaryzacja, y)) 
    { 
     for(x = x2 = 0; x < width; x++) 
     { 
      if (!IsColumnEmpty(binaryzacja, x)) 
      { 
       binaryzacja2[x2, y2] = binaryzacja[x, y]; 
       x2++; 
      } 
     } 
     y2++; 
    } 
} 



bool IsRowEmpty(int[,] array, int y) 
{ 
    for (int x = 0; x < array.GetLength(0); x++) 
    { 
     if (array[x, y] != 0) return false; 
    } 
    return true; 
} 

bool IsColumnEmpty(int[,] array, int x) 
{ 
    for (int y = 0; y < array.GetLength(1); y++) 
    { 
     if (array[x, y] != 0) return false; 
    } 
    return true; 
} 

-----新版本:

int width = binaryzacja.GetLength(0); 
int height = binaryzacja.GetLength(1); 

int newWidth = width; 
int newHeight = height; 

int[] keepRows = new int[height]; 
int[] keepColumns = new int[width]; 

int x, y, x2, y2; 
int i; 

for (i = y = 0; y < height; y++) 
{ 
    if (IsRowEmpty(binaryzacja, y)) newHeight--; 
    else 
    { 
     keepRows[i] = y; 
     i++; 
    } 
} 

for (i = x = 0; x < width; x++) 
{ 
    if (IsColumnEmpty(binaryzacja, x)) newWidth--; 
    else 
    { 
     keepColumns[i] = x; 
     i++; 
    } 
} 

int[,] binaryzacja2 = new int[newWidth, newHeight]; 

// copy to new array 
for (y2 = y = 0; y < height; y++) 
{ 
    if(y == keepRows[y2]) 
    { 
     for (x2 = x = 0; x < width; x++) 
     { 
      if(x == keepColumns[x2]) 
      { 
       binaryzacja2[x2, y2] = binaryzacja[x, y]; 
       x2++; 
      } 
     } 
     y2++; 
    } 
} 
+0

考慮爲您的代碼提供解釋 – arghtype 2014-11-01 21:06:27

+0

也許效率更高...關於評論?難道幾乎不是自殺嗎? – 2014-11-01 21:10:50