Created
November 30, 2015 21:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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