Skip to contents

stm_rot90() rotates a matrix by 90 degrees counterclockwise. The number of 90-degree rotations is determined by the argument k.

Usage

stm_rot90(mat, k = 1)

Arguments

mat

A numeric matrix to be rotated.

k

The number of 90-degree rotations. Default is 1, which rotates the matrix once. The matrix can be rotated multiple times by specifying higher values for k.

Value

A rotated matrix.

Details

The function supports multiple rotations by accepting an integer k. The matrix is rotated counterclockwise by 90 degrees for each rotation. The parameter k is modulo 4 to handle the cases where more than 4 rotations are requested.

Examples

A <- matrix(1:12, nrow = 3, byrow = TRUE)
print(A)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12
stm_rot90(A)
#>      [,1] [,2] [,3]
#> [1,]    4    8   12
#> [2,]    3    7   11
#> [3,]    2    6   10
#> [4,]    1    5    9
stm_rot90(A, k = 2)
#>      [,1] [,2] [,3] [,4]
#> [1,]   12   11   10    9
#> [2,]    8    7    6    5
#> [3,]    4    3    2    1
stm_rot90(A, k = -2)
#>      [,1] [,2] [,3] [,4]
#> [1,]   12   11   10    9
#> [2,]    8    7    6    5
#> [3,]    4    3    2    1
stm_rot90(A, k = 3)
#>      [,1] [,2] [,3]
#> [1,]    9    5    1
#> [2,]   10    6    2
#> [3,]   11    7    3
#> [4,]   12    8    4
stm_rot90(A, k = 4) # original matrix
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12