Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created October 19, 2015 18:08
Show Gist options
  • Save primaryobjects/2a4e3b5f6b6776c9676b to your computer and use it in GitHub Desktop.
Save primaryobjects/2a4e3b5f6b6776c9676b to your computer and use it in GitHub Desktop.
Mining Massive Datasets - Map Reduce 6a
# mmds Week6A
# Q1
# Using the matrix-vector multiplication described in Section 2.3.1, applied to the matrix and vector:
# 1 2 3 4
# 5 6 7 8
# 9 10 11 12
# 13 14 15 16
#
# 1
# 2
# 3
# 4
# apply the Map function to this matrix and vector. Then, identify in the list below, one of the key-value pairs that are output of Map.
#
x <- matrix(c(1, 5, 9, 13, 2,6,10,14,3,7,11,15,4,8,12,16), nrow=4, ncol=4)
y <- matrix(1:4, nrow=4, ncol=1)
# Multiply matrix by vector.
reduce <- sweep(x, MARGIN=2, y, '*')
# List map/reduce pairs.
row <- 1
count <- 0
pairs <- sapply(seq_along(reduce), function(index) {
if (count == 4) {
count <<- 0
row <<- row + 1
}
count <<- count + 1
list(c(row, t(reduce)[index]))
})
pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment