Nested loops and debugging

Purpose: Develop a function that returns a 4x4 array. An example of a possible function result is:

All I have done:

matriz<-function(M){

M<-matrix(ncol=4,nrow=4);

M[1,]<-sample(LETTERS[1:4]);

M[2,]<-sample(LETTERS[1:4]);

for(i in 2:4){
    for(j in 1:4){
    j<-j

        if(j<=4)
        if(M[i,j]!= M[i-1,j]){
            j<-j+1
            }
        else{
            M[i,]<-sample(LETTERS[1:4])
            }

    }
    i<-i+1
    if(i<=4){
        M[i,]<-sample(LETTERS[1:4])
        j=1
        }
    else{print(M)}
 }
}

debug(matriz); matriz(M)
+6
source share
2 answers

It seems to work. It generates a list of all possible combinations, then removes everything that matches the previously selected lines, leaving only one at the end.

matriz <- function(n){
  combs <- as.matrix(expand.grid(rep(list(LETTERS[1:n]),n)))
  combs <- combs[apply(combs,1,function(r) all(LETTERS[1:n] %in% r)),]
  mat <- matrix(NA,nrow=n,ncol=n)
  for(i in 1:(n-1)){
    mat[i,] <- combs[sample(1:nrow(combs),1),]
    combs <- combs[!apply(combs,1,function(r) any(r == mat[i,])),]
  }
  mat[n,] <- combs
  return(mat)
  }

> matriz(5)
     [,1] [,2] [,3] [,4] [,5]
[1,] "B"  "D"  "A"  "E"  "C" 
[2,] "E"  "C"  "D"  "B"  "A" 
[3,] "D"  "A"  "B"  "C"  "E" 
[4,] "A"  "E"  "C"  "D"  "B" 
[5,] "C"  "B"  "E"  "A"  "D" 

> matriz(5)
     [,1] [,2] [,3] [,4] [,5]
[1,] "D"  "C"  "E"  "B"  "A" 
[2,] "E"  "A"  "C"  "D"  "B" 
[3,] "A"  "D"  "B"  "C"  "E" 
[4,] "B"  "E"  "D"  "A"  "C" 
[5,] "C"  "B"  "A"  "E"  "D" 

A slightly faster version with a package combinatwould be

library(combinat)
matriz <- function(n){
  combs <- do.call(rbind,permn(LETTERS[1:n]))
  mat <- matrix(NA,nrow=n,ncol=n)
  #rest of function as above...

Both can be quite slow for n> 10 or so. However, if you created one real square m, then all the others will be permutations m[sample(nrow(m)),sample(ncol(m))], so this might be a faster approach if you make a lot of them.

+2

MUCH, . , .

. , @alexis_laz.

matriz <- function(n){
  l <- rep(LETTERS[1:n],each=n)
  mat <- matrix(NA,nrow=n,ncol=n)
  i<-1
  j<-1
  for(k in 1:(n*n)){
    blank=is.na(mat[i,j])
    while(!blank){
      j <- j%%n+1
      blank=is.na(mat[i,j])
    }
    mat[i,j] <- l[k]
    i <- i%%n+1
    j <- j%%n+1
  }
  return(mat[sample(n),sample(n)])
}

matriz(15)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
 [1,] "E"  "H"  "G"  "M"  "K"  "F"  "N"  "O"  "L"  "B"   "A"   "D"   "J"   "I"   "C"  
 [2,] "F"  "I"  "H"  "N"  "L"  "G"  "O"  "A"  "M"  "C"   "B"   "E"   "K"   "J"   "D"  
 [3,] "I"  "L"  "K"  "B"  "O"  "J"  "C"  "D"  "A"  "F"   "E"   "H"   "N"   "M"   "G"  
 [4,] "A"  "D"  "C"  "I"  "G"  "B"  "J"  "K"  "H"  "M"   "L"   "O"   "F"   "E"   "N"  
 [5,] "G"  "J"  "I"  "O"  "M"  "H"  "A"  "B"  "N"  "D"   "C"   "F"   "L"   "K"   "E"  
 [6,] "J"  "M"  "L"  "C"  "A"  "K"  "D"  "E"  "B"  "G"   "F"   "I"   "O"   "N"   "H"  
 [7,] "B"  "E"  "D"  "J"  "H"  "C"  "K"  "L"  "I"  "N"   "M"   "A"   "G"   "F"   "O"  
 [8,] "L"  "O"  "N"  "E"  "C"  "M"  "F"  "G"  "D"  "I"   "H"   "K"   "B"   "A"   "J"  
 [9,] "K"  "N"  "M"  "D"  "B"  "L"  "E"  "F"  "C"  "H"   "G"   "J"   "A"   "O"   "I"  
[10,] "H"  "K"  "J"  "A"  "N"  "I"  "B"  "C"  "O"  "E"   "D"   "G"   "M"   "L"   "F"  
[11,] "D"  "G"  "F"  "L"  "J"  "E"  "M"  "N"  "K"  "A"   "O"   "C"   "I"   "H"   "B"  
[12,] "C"  "F"  "E"  "K"  "I"  "D"  "L"  "M"  "J"  "O"   "N"   "B"   "H"   "G"   "A"  
[13,] "N"  "B"  "A"  "G"  "E"  "O"  "H"  "I"  "F"  "K"   "J"   "M"   "D"   "C"   "L"  
[14,] "M"  "A"  "O"  "F"  "D"  "N"  "G"  "H"  "E"  "J"   "I"   "L"   "C"   "B"   "K"  
[15,] "O"  "C"  "B"  "H"  "F"  "A"  "I"  "J"  "G"  "L"   "K"   "N"   "E"   "D"   "M" 
+1

Source: https://habr.com/ru/post/1017234/


All Articles