Skip to content

Instantly share code, notes, and snippets.

@WhiskersReneeWe
Created May 20, 2019 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhiskersReneeWe/98e802300c48f1505e000144ce8089fe to your computer and use it in GitHub Desktop.
Save WhiskersReneeWe/98e802300c48f1505e000144ce8089fe to your computer and use it in GitHub Desktop.
Shrink a 28 * 28 matrix to 14 * 14 in R
# Divide each 28*28 image into 2*2 non-overlapping blocks.
shrink_img <- function(img){
# input: 28 * 28 image
# window size is 2 * 2, move horizontally -- fix row first, move across columns
# output: 14 * 14 image
shrinked_img = matrix(, nrow = 14, ncol = 14)
for (i in c(0:13)) {
for (j in c(0:13)){
row = 1 + 2 * i
col = 1 + 2 * j
small_block = img[row:row+1, col:col+1]
ave = sum(small_block)/4
# store it
shrinked_img[i+1, j+1] = ave
}
}
return(shrinked_img)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment