Skip to content

Instantly share code, notes, and snippets.

@seankross
Created November 30, 2015 21:52
Show Gist options
  • Save seankross/014177d3a92384b90aed to your computer and use it in GitHub Desktop.
Save seankross/014177d3a92384b90aed to your computer and use it in GitHub Desktop.
Kross-Kross algorithm for filling a matrix with the coordinates of another matrix
A <- matrix(NA, nrow = 4, ncol = 7)
x <- function(A){
total_pairs <- nrow(A) * ncol(A)
rc <- matrix(NA, ncol = 2, nrow = total_pairs)
k <- r <- c <- 1
while (k <= total_pairs) {
rc[k, 1] <- r
rc[k, 2] <- c
k <- k+1
if(c >= ncol(A)){
r <- r+1
c <- 1
} else {
c <- c+1
}
}
rc
}
x(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment