[R] non-isomorphic sequences

Petr Savicky savicky at cs.cas.cz
Mon Feb 13 23:38:03 CET 2012


On Mon, Feb 13, 2012 at 02:04:51PM -0800, zheng wei wrote:
> Dear Petr,
>  
> This is fantastic!
>  
> I have one more question, when p=4, tt=4. We have 15 non-isomorphic sequences as you have generated. Among these 15, I selected 2 sequences. How do I recover all the members of the equivalent classes corresponding to these 2 sequences? For example, corresponding to the sequence of 1111, I would like to recover 1111,2222,3333,4444 from this sequence.

Dear Wei:

Try the following.

  getEquivalent <- function(a, tt)
  {
      b <- as.matrix(rev(expand.grid(rep(list(1:tt), times=max(a)))))
      ok <- apply(b, 1, function(x) length(unique(x))) == ncol(b)
      b <- b[ok, , drop=FALSE]
      dimnames(b) <- NULL
      t(apply(b, 1, function(x) x[a]))
  }

  getEquivalent(c(1, 1, 1, 1), 4)

       [,1] [,2] [,3] [,4]
  [1,]    1    1    1    1
  [2,]    2    2    2    2
  [3,]    3    3    3    3
  [4,]    4    4    4    4

  getEquivalent(c(1, 1, 1, 2), 4)

        [,1] [,2] [,3] [,4]
   [1,]    1    1    1    2
   [2,]    1    1    1    3
   [3,]    1    1    1    4
   [4,]    2    2    2    1
   [5,]    2    2    2    3
   [6,]    2    2    2    4
   [7,]    3    3    3    1
   [8,]    3    3    3    2
   [9,]    3    3    3    4
  [10,]    4    4    4    1
  [11,]    4    4    4    2
  [12,]    4    4    4    3

Hope this helps.

Petr.



More information about the R-help mailing list