r - 构建一个没有重复但固定部分输入的随机矩阵

标签 r matrix backtracking recursive-backtracking

我在构建一个随机矩阵时遇到了问题,其中我部分已经有了值(需要保持固定 - 所以没有进一步的随机化)。

让我们来看看:

矩阵最终应该是 10 x 10

 n <- 10 

我确实希望我的第一行是我输入的数据。例如:
  row1<- c(1,4,7,6,5,3,2,8,9,10)
  row2<- c(10,7,3,2,1,4,5,9,8,6)
  row3<- c(9,2,4,3,8,7,10,1,6,5)

为了绘制一个 10 行(和 10 列)的矩阵,我将这些行与样本组合在一起(没有替换,因为我希望每个数字在每一行中都是唯一的)。
 first.rows<-rbind(row1,row2,row3,sample(n,n,replace=F),sample(n,n,replace=F),sample(n,n,replace=F),sample(n,n,replace=F),sample(n,n,replace=F),sample(n,n,replace=F),sample(n,n,replace=F))

输出:
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
row1    1    4    7    6    5    3    2    8    9    10
row2   10    7    3    2    1    4    5    9    8     6
row3    9    2    4    3    8    7   10    1    6     5
        6    1    5    4    2   10    3    8    7     9
        2    5    7    8    9    6    1    3    4    10
       10    6    4    1    8    3    7    2    5     9
        8    5    3    2    4    1   10    7    6     9
       10    7    9    6    8    2    5    4    3     1
        1   10    8    4    7    3    5    2    6     9
        2    1   10    4    8    9    3    6    5     7

到现在为止还挺好..
但是现在我遇到了无法控制列中唯一数字的问题。不过,这正是我所需要的。我明白这是因为我使用了 rbind (因此只有无重复的功能仅适用于行)。但我不知道如何解决这个问题。第 1-3 行应保持原样。

有任何想法吗?

最佳答案

我想我以前的解决方案 Fixed values not repeated over column and row可以修改工作。您需要一个求解器,但不是从空网格开始,而是从预填充矩阵开始:

# x is your matrix, "not filled" values should be NA
# x is a square matrix with dimension n (big n will take longer to converge)
backtrack = function(x){
  n = ncol(x)
  stopifnot(ncol(x)==nrow(x))

  cells = list()
  k = 1
  for (i in 1:n){
    for (j in 1:n){

      if (is.na(x[i, j]))
        cells[[k]] = sample(1:n)
      else
        cells[[k]] = NULL
      k = k + 1
    }
  }

  i = 0
  while (i < n*n){

    if (is.null(cells[[i+1]])){
        i=i+1
        next
    }

    candidates = cells[[i + 1]]
    idx = sample(1:length(candidates), 1)
    val = candidates[idx]

    if (length(candidates) == 0){
      cells[[i + 1]] = sample(1:n)
      i = i - 1
      x[as.integer(i/n) + 1,  i %% n + 1] = NA
    }
    else {
      rr = as.integer(i/n) + 1
      cc = i %% n + 1
      if ((val %in% x[rr, ]) || (val %in% x[, cc])){
        candidates = candidates[-idx]
        cells[[i + 1]] = candidates
      }
      else{
        x[as.integer(i/n) + 1, i %% n + 1] = val
        candidates = candidates[-idx]
        cells[[i + 1]] = candidates
        i = i + 1
      }
    }
  }
  x
}

空初始矩阵
set.seed(1)
x = backtrack(matrix(NA, nrow = 10, ncol = 10))
print(x)
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    8   10    4    6    9    7    1    2    3     5
 [2,]    5    6    9    8    1   10    4    3    2     7
 [3,]   10    7    1    2    8    9    5    4    6     3
 [4,]    3    9    8   10    6    5    7    1    4     2
 [5,]    9    1    6    4    7    3    2    5   10     8
 [6,]    1    4   10    3    2    6    8    7    5     9
 [7,]    2    8    5    9   10    1    3    6    7     4
 [8,]    6    5    2    7    3    4   10    9    8     1
 [9,]    4    3    7    1    5    2    6    8    9    10
[10,]    7    2    3    5    4    8    9   10    1     6

预填充的初始矩阵
m = matrix(NA, ncol = 10, nrow = 10)
m[1, ] = c(1,4,7,6,5,3,2,8,9,10)
m[2, ] = c(10,7,3,2,1,4,5,9,8,6)
m[3, ] = c(9,2,4,3,8,7,10,1,6,5)

x = backtrack(m)
print(x)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    4    7    6    5    3    2    8    9    10
 [2,]   10    7    3    2    1    4    5    9    8     6
 [3,]    9    2    4    3    8    7   10    1    6     5
 [4,]    5    9    6    8    3    2    4    7   10     1
 [5,]    7    1    5   10    9    6    3    2    4     8
 [6,]    2    5    8    1   10    9    6    3    7     4
 [7,]    6    3    1    4    7    5    8   10    2     9
 [8,]    8   10    9    5    4    1    7    6    3     2
 [9,]    3    6   10    9    2    8    1    4    5     7
[10,]    4    8    2    7    6   10    9    5    1     3

注意:我没有测试它的错误。

关于r - 构建一个没有重复但固定部分输入的随机矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186549/

相关文章:

R - 基于另一列以差异方式创建新列

r - 在 R 中打印没有前导方括号的结果

C - 数组内容交换

matlab - 矩阵 "Zigzag"重新排序

java - Leetcode 351 Android 解锁模式

python - 如何生成在向量的数字之间添加 + 和 - 的所有可能性,因此总和应该是正数,使用回溯

Prolog GNU - Univ 运营商?对它的解释

r - 如何在 R 中读取 Whatsapp 电子邮件文件?

c - C中的结构矩阵

R:分组表中缺失级别的零填充