简体   繁体   中英

How can I use for loops to construct a matrix with a modular pattern in R?

I want to have a (q^p) xp matrix, where q is a prime number.

And I want to construct a pattern such as below if q=3, p=3:

0  0  0
1  0  0
2  0  0
0  1  0
1  1  0
2  1  0
0  2  0
1  2  0
2  2  0
0  0  1
1  0  1
2  0  1
0  1  1
1  1  2
0  2  1
0  0  2
1  0  2
2  0  2
0  1  2
1  1  2
0  2  2
1  2  2
2  2  2

The code that I have so far is:

  F <- matrix(ncol=p,nrow=q^p) #initialize the vector F
  for(i in 1:(q^p)){
    for(j in 1:p){  
      F[i,j] <- ((i-1) %% q) * (j %% 3)
    }
  }

In case another example would be helpful, here's what aq=2, p=3 matrix should look like:

0  0  0
1  0  0
0  1  0
1  1  0
0  0  1
1  0  1
0  1  1
1  1  1

I know that I'll need to use some modular functions, but I am not sure how to achieve this problem.

using expand.grid

f = function(q, p) {
  as.matrix(expand.grid(replicate(p, 1:q-1L, FALSE)))
}


f(2,3)
#      Var1 Var2 Var3
# [1,]    0    0    0
# [2,]    1    0    0
# [3,]    0    1    0
# [4,]    1    1    0
# [5,]    0    0    1
# [6,]    1    0    1
# [7,]    0    1    1
# [8,]    1    1    1

f(3,3)
#      Var1 Var2 Var3
# [1,]    0    0    0
# [2,]    1    0    0
# [3,]    2    0    0
# [4,]    0    1    0
# [5,]    1    1    0
# [6,]    2    1    0
# [7,]    0    2    0
# [8,]    1    2    0
# [9,]    2    2    0
# [10,]    0    0    1
# [11,]    1    0    1
# [12,]    2    0    1
# [13,]    0    1    1
# [14,]    1    1    1
# [15,]    2    1    1
# [16,]    0    2    1
# [17,]    1    2    1
# [18,]    2    2    1
# [19,]    0    0    2
# [20,]    1    0    2
# [21,]    2    0    2
# [22,]    0    1    2
# [23,]    1    1    2
# [24,]    2    1    2
# [25,]    0    2    2
# [26,]    1    2    2
# [27,]    2    2    2

This computes a single iteration for each sequence, then relies on recycling to fill out the length.

foo = function(p, q) {
  seqs = lapply(1:p, function(x) rep((1:q) - 1, each = q^(x - 1)))
  do.call(cbind, seqs)
}

> foo(3, 2)
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    1    0    0
[3,]    0    1    0
[4,]    1    1    0
[5,]    0    0    1
[6,]    1    0    1
[7,]    0    1    1
[8,]    1    1    1

Here is another method for Galois field characterizations (but not as simple as the method by @Gregor Thomas )

gf <- function(q,p) {
  x <- seq(q**p)-1
  t(sapply(x, function(v) {
    if (v==0) {
      r <- 0
    } else {
      r <- c()
      while (v!=0) {
        r <- c(v%%q,r)
        v <- v%/%q 
      }
    }
    c(rep(0,p-length(r)),r)}))
}

such that

> q <- 3

> p <- 3

> gf(p,q)
      [,1] [,2] [,3]
 [1,]    0    0    0
 [2,]    0    0    1
 [3,]    0    0    2
 [4,]    0    1    0
 [5,]    0    1    1
 [6,]    0    1    2
 [7,]    0    2    0
 [8,]    0    2    1
 [9,]    0    2    2
[10,]    1    0    0
[11,]    1    0    1
[12,]    1    0    2
[13,]    1    1    0
[14,]    1    1    1
[15,]    1    1    2
[16,]    1    2    0
[17,]    1    2    1
[18,]    1    2    2
[19,]    2    0    0
[20,]    2    0    1
[21,]    2    0    2
[22,]    2    1    0
[23,]    2    1    1
[24,]    2    1    2
[25,]    2    2    0
[26,]    2    2    1
[27,]    2    2    2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM