Here is yet one more simplification. Note that seq9 in this one is not the same as seq9 in the last one. This one looks much more symmetric in the arguments and is shorter: seq9 <- function(i) seq(0, len = 9, by = i) jj <- function(i,j) outer( seq9(i), seq9(j), "+" ) %% 9 %/% 3 + 1 jj1. <- jj(3,1) jj2. <- jj(3,5) jj3. <- jj(4,1)