2017-09-12 21 views
0

及彼很大的幫助,隨機矩陣沒有重複的行和列(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

+0

您可以指定行索引以防止函數混洗第1-3行,例如, '矩陣[4:nrow(矩陣),]' –

+0

謝謝!但是我會在哪裏實現這個規範?由於我已經需要在偏移之前修正行...請參閱上面的編輯以更好地描述問題 – shampoo

回答

0

將這項工作?

mat <- matrix(1:100,10) 
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] 
# [1,] 1 11 21 31 41 51 61 71 81 91 
# [2,] 2 12 22 32 42 52 62 72 82 92 
# [3,] 3 13 23 33 43 53 63 73 83 93 
# [4,] 6 16 26 36 46 56 66 76 86 96 
# [5,] 10 20 30 40 50 60 70 80 90 100 
# [6,] 7 17 27 37 47 57 67 77 87 97 
# [7,] 4 14 24 34 44 54 64 74 84 94 
# [8,] 5 15 25 35 45 55 65 75 85 95 
# [9,] 9 19 29 39 49 59 69 79 89 99 
# [10,] 8 18 28 38 48 58 68 78 88 98 
+0

謝謝!我試過這個,但是我得到了一個「越界越界」的錯誤....請參閱上面的編輯以更好地描述問題! – shampoo

+0

有3次失誤遺憾:)。它現在已經修復了! –

+0

太棒了!非常感謝你!!這現在很好用!你也許也知道如何解決我的問題與列中的重複值(請參閱原始問題編輯中的問題1)??我明白爲什麼它首先返回我的重複項(因爲我使用rbind),但是我不知道如何避免這個問題... – shampoo

相關問題