及彼很大的帮助,随机矩阵没有重复的行和列(Fixed values not repeated over column and row)我有一个关于修改另一个问题之后。的R - 为固定值添加随机值未在列重复矩阵和行
所以,先下手做了什么:我想与随机的行和列的矩阵无需在每个重复(行方向和列)明智的。再次感谢这里本有很大的帮助是jdobres代码(https://stackoverflow.com/users/6436545/jdobres)我最终使用:
# number of rows and columns
n <- 10
# create ordered rows and columns
ordered.by.row <- matrix(1:n, n, n)
ordered.by.col <- matrix(1:n, n, n, byrow = T)
# offset the rows and columns relative to each other.
# no row or column has a repeated value, but the values are still ordered
offset <- (ordered.by.row + ordered.by.col) %% n + 1
# shuffle the columns, then shuffle the rows, this produces a randomized matrix
# 'shuffle.row' is the final, randomized matrix
set.seed(1222) # change this to change randomization
shuffle.col <- offset[,sample(1:n, n, replace = F)]
shuffle.row <- shuffle.col[sample(1:n, n, replace = F), ]
# verify solution
any(apply(shuffle.row, 1, function(r)any(duplicated(r)))) # FALSE
any(apply(shuffle.row, 2, function(r)any(duplicated(r)))) # FALSE
> # create ordered rows and columns
ordered.by.row <- matrix(1:n, n, n)
ordered.by.col <- matrix(1:n, n, n, byrow = T)
# offset the rows and columns relative to each other.
# no row or column has a repeated value, but the values are still ordered
offset <- (ordered.by.row + ordered.by.col) %% n + 1
# shuffle the columns, then shuffle the rows, this produces a randomized matrix
# 'shuffle.row' is the final, randomized matrix
set.seed(1222) # change this to change randomization
shuffle.col <- offset[,sample(1:n, n, replace = F)]
shuffle.row <- shuffle.col[sample(1:n, n, replace = F), ]
# verify solution
any(apply(shuffle.row, 1, function(r)any(duplicated(r)))) # FALSE
any(apply(shuffle.row, 2, function(r)any(duplicated(r)))) # FALSE
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 10 6 9 2 8 3 5 7 4
[2,] 3 2 8 1 4 10 5 7 9 6
[3,] 7 6 2 5 8 4 9 1 3 10
[4,] 9 8 4 7 10 6 1 3 5 2
[5,] 10 9 5 8 1 7 2 4 6 3
[6,] 2 1 7 10 3 9 4 6 8 5
[7,] 8 7 3 6 9 5 10 2 4 1
[8,] 6 5 1 4 7 3 8 10 2 9
[9,] 5 4 10 3 6 2 7 9 1 8
[10,] 4 3 9 2 5 1 6 8 10 7
但是我现在,我想我的第一行有问题(可以说排1,2,3)按特定顺序修复。不过,我需要新的行4至10再次随机分组并没有(关于1-3行也没有重复)重复!
所以,比如我有3行是这样的:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 8 7 3 6 9 5 10 2 4 1
[2,] 6 5 1 4 7 3 8 10 2 9
[3,] 5 4 10 3 6 2 7 9 1 8
但接下来的7行我想随机但不重复(在任何行或列)值,而不改变第一行...
任何想法?我无法弄清楚如何排除那些我想留下来(第1-3行),但仍然没有在其他行和列的重复...
您的帮助将再次非常感谢!
编辑:
非常感谢您Moody_Mudskipper您的帮助!这是什么解决方案看起来就像用我的(假的)数据:
mat<-as.matrix(first.rows)
nkeep <- 3
mat_shuffled <- mat[c(1:nkeep,sample((nkeep+1):nrow(mat),replace=FALSE)),]
[,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 5 7 10 1 6 5
10 7 9 6 8 2 5 4 3 1
1 10 8 4 7 3 5 2 6 9
10 6 4 1 8 3 7 2 5 9
2 5 7 8 9 6 1 3 4 10
2 1 10 4 8 9 3 6 5 7
8 5 3 2 4 1 10 7 6 9
6 1 5 4 2 10 3 8 7 9
还得感谢你!
查阅Constructing a randomised matrix with no duplicates but fixed partial input一个解决方案费尔南多也保持VALUES列UNIQUE
您可以指定行索引以防止函数混洗第1-3行,例如, '矩阵[4:nrow(矩阵),]' –
谢谢!但是我会在哪里实现这个规范?由于我已经需要在偏移之前修正行...请参阅上面的编辑以更好地描述问题 – shampoo