列的最佳組合,假設我有2000列的大型矩陣(matrix_1)。每個單元格的值爲0或1.我想要找到10列的最佳組合。最佳組合給出每行非0值的最大數量。因此,它基本上提供了最大的找到一個矩陣
sum (apply (matrix_2, 1, function(x) any(x == 1)))
我不能去通過所有可能的組合,因爲它是計算量太大(有2.758988e + 26)。有什麼建議麼?
舉一個例子藉此矩陣具有4行,我一次只
mat <- matrix (c(1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0), nrow = 4, byrow = FALSE)
mat
# combination of columns 2 and 3 is best: 3 rows with at least a single 1 value
sum (apply (mat[, c(2, 3)], 1, function(x) any (x == 1)))
# combination of columns 1 and 2 is worse: 2 rows with at least a single 1 value
sum (apply (mat[, c(1, 2)], 1, function(x) any (x == 1)))
在你的矩陣有多少行? – CPak
100-200行。取決於應用程序通過'colSums(COL)' –
你不能爲了你的列並選擇前10名?我問,因爲我不是100%確定你想要什麼,這有助於我更好地瞭解你在找什麼。 – CPak