Created
May 20, 2019 18:25
-
-
Save WhiskersReneeWe/98e802300c48f1505e000144ce8089fe to your computer and use it in GitHub Desktop.
Shrink a 28 * 28 matrix to 14 * 14 in R
This file contains hidden or 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
# 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