This function rotates the values in a matrix by a user-specified number of degrees. In almost all cases some values will fall outside the matrix so they will be discarded. Cells that have no rotated values will become NA
. Only square matrices can be accommodated. In some cases a rotation will cause cells to have no assigned value because no original values fall within them. In these instances the mean value of surrounding cells is assigned to the cells with missing values. If the angle of rotation is too small then no rotation will occur.
Examples
x <- matrix(1:100, nrow=10)
x
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 11 21 31 41 51 61 71 81 91
#> [2,] 2 12 22 32 42 52 62 72 82 92
#> [3,] 3 13 23 33 43 53 63 73 83 93
#> [4,] 4 14 24 34 44 54 64 74 84 94
#> [5,] 5 15 25 35 45 55 65 75 85 95
#> [6,] 6 16 26 36 46 56 66 76 86 96
#> [7,] 7 17 27 37 47 57 67 77 87 97
#> [8,] 8 18 28 38 48 58 68 78 88 98
#> [9,] 9 19 29 39 49 59 69 79 89 99
#> [10,] 10 20 30 40 50 60 70 80 90 100
rotateMatrix(x, 90) # 90 degrees to the right
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 10 9 8 7 6 5 4 3 2 1
#> [2,] 20 19 18 17 16 15 14 13 12 11
#> [3,] 30 29 28 27 26 25 24 23 22 21
#> [4,] 40 39 38 37 36 35 34 33 32 31
#> [5,] 50 49 48 47 46 45 44 43 42 41
#> [6,] 60 59 58 57 56 55 54 53 52 51
#> [7,] 70 69 68 67 66 65 64 63 62 61
#> [8,] 80 79 78 77 76 75 74 73 72 71
#> [9,] 90 89 88 87 86 85 84 83 82 81
#> [10,] 100 99 98 97 96 95 94 93 92 91
rotateMatrix(x, 180) # 180 degrees to the right
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 100 90 80 70 60 50 40 30 20 10
#> [2,] 99 89 79 69 59 49 39 29 19 9
#> [3,] 98 88 78 68 58 48 38 28 18 8
#> [4,] 97 87 77 67 57 47 37 27 17 7
#> [5,] 96 86 76 66 56 46 36 26 16 6
#> [6,] 95 85 75 65 55 45 35 25 15 5
#> [7,] 94 84 74 64 54 44 34 24 14 4
#> [8,] 93 83 73 63 53 43 33 23 13 3
#> [9,] 92 82 72 62 52 42 32 22 12 2
#> [10,] 91 81 71 61 51 41 31 21 11 1
rotateMatrix(x, 45) # 45 degrees to the right
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] NA NA 4 3.000 13.00000 22.000 21.000 31 NA NA
#> [2,] NA 7.6 5 14.000 19.75000 23.000 32.000 41 47.200 NA
#> [3,] 7 6.0 16 25.000 24.00000 34.000 43.000 52 51.000 61
#> [4,] 8 17.0 26 27.000 35.00000 44.000 48.500 53 62.000 71
#> [5,] 19 24.0 27 36.000 44.14286 45.000 54.000 63 68.875 72
#> [6,] 29 28.0 38 47.000 46.00000 56.000 65.000 74 73.000 83
#> [7,] 30 39.0 48 52.625 57.00000 66.000 71.875 75 84.000 93
#> [8,] 40 50.0 59 58.000 68.00000 77.000 76.000 86 95.000 94
#> [9,] NA 55.8 60 69.000 78.00000 82.625 87.000 96 93.600 NA
#> [10,] NA NA 70 80.000 89.00000 88.000 98.000 97 NA NA
rotateMatrix(x, 7) # slight rotation
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 2 NA 11 21 31 41 51 61 71 81
#> [2,] 3 12 22 32 42 52 62 72 82 NA
#> [3,] 4 13 23 33 43 53 63 73 83 92
#> [4,] 5 14 24 34 44 54 64 74 84 93
#> [5,] 6 15 25 35 45 55 65 75 85 94
#> [6,] 7 16 26 36 46 56 66 76 86 95
#> [7,] 8 17 27 37 47 57 67 77 87 96
#> [8,] 9 18 28 38 48 58 68 78 88 97
#> [9,] NA 19 29 39 49 59 69 79 89 98
#> [10,] 20 30 40 50 60 70 80 90 NA 99
rotateMatrix(x, 5) # no rotation because angle is too small
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 11 21 31 41 51 61 71 81 91
#> [2,] 2 12 22 32 42 52 62 72 82 92
#> [3,] 3 13 23 33 43 53 63 73 83 93
#> [4,] 4 14 24 34 44 54 64 74 84 94
#> [5,] 5 15 25 35 45 55 65 75 85 95
#> [6,] 6 16 26 36 46 56 66 76 86 96
#> [7,] 7 17 27 37 47 57 67 77 87 97
#> [8,] 8 18 28 38 48 58 68 78 88 98
#> [9,] 9 19 29 39 49 59 69 79 89 99
#> [10,] 10 20 30 40 50 60 70 80 90 100