w3resource

R Programming: Rotate a given matrix 90 degree clockwise rotation

R Programming: Matrix Exercise-12 with Solution

Write a R program to rotate a given matrix 90 degree clockwise rotation.

Sample Solution:

R Programming Code:

x =  matrix(1:9, 3)
print("Original matrix:")
print(x)
rotate = t(apply(x, 2, rev))
print("Rotate the said matrix 90 degree clockwise:")
print(rotate)

Sample Output:

[1] "Original matrix:"
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[1] "Rotate the said matrix 90 degree clockwise:"
     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    6    5    4
[3,]    9    8    7               

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to find row and column index of maximum and minimum value in a given matrix.
Next: Write a R program to concatenate two given matrixes of same column but different rows.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.