簡體   English   中英

查找介於1和k之間的n個數字的所有唯一組合

[英]find all unique combinations of n numbers between 1 and k

我想要一個在1到63之間(或更籠統地說是1到k)的五個(或n個)數字的所有可能集合的列表

如果計算時間不是問題,我可以做類似的事情

 #Get all combenations of numbers between 1 and 63
 indexCombinations <- expand.grid(1:63, 1:63, 1:63, 1:63, 1:63)

 #Throw out the rows that have more than one of the same number in them
 allDifferent <- apply(indexCombinations, 1, function(x){
      length(x) == length(unique(x))
 } # function
 ) # apply

 indexCombinationsValid <- indexCombinations[allDifferent,]

 # And then just take the unique values
 indexCombinationsValidUnique <- unique(indexCombinationsValid)

我擔心,發現獨特價值的步伐將非常緩慢。 此外,我最終不得不一排排從未使用過的行。 我想知道是否有人有一種更優雅,更有效的方法來獲取一個值和某個值范圍之間的五個數字(或n個數字)的唯一組合的數據框或矩陣。

感謝@SymbolixAU提供一個非常優雅的解決方案,我將其在此處重新發布作為答案:

 n <- 1:63; x <- combn(n, m = 5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM