Skip to content

Instantly share code, notes, and snippets.

@ericnovik
Last active May 8, 2018 00:24
Show Gist options
  • Save ericnovik/d4af8493e0824ab48ca7866fced0e187 to your computer and use it in GitHub Desktop.
Save ericnovik/d4af8493e0824ab48ca7866fced0e187 to your computer and use it in GitHub Desktop.
x <- matrix(rep(10, 12), nrow = 4)
y <- c(2, 5, 10)
div_matrix_by_vec <- function(mat, vec) {
stopifnot(is.matrix(mat))
stopifnot(is.numeric(vec))
stopifnot(ncol(mat) == length(vec))
t(apply(mat, 1, function(x) x/vec))
}
all.equal(div_matrix_by_vec(x, y),
matrix(c(5,5,5,5,2,2,2,2,1,1,1,1),
nrow = 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment