Rotate a Matrix in R by 90 Degrees Clockwise

Rotate a Matrix in R by 90 degrees clockwise

t does not rotate the entries, it flips along the diagonal:

x <- matrix(1:9, 3)
x
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9

t(x)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9

90 degree clockwise rotation of R matrix:

You need to also reverse the columns prior to the transpose:

rotate <- function(x) t(apply(x, 2, rev))
rotate(x)
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 6 5 4
## [3,] 9 8 7

rotate(rotate(x))
## [,1] [,2] [,3]
## [1,] 9 6 3
## [2,] 8 5 2
## [3,] 7 4 1

rotate(rotate(rotate(x)))
## [,1] [,2] [,3]
## [1,] 7 8 9
## [2,] 4 5 6
## [3,] 1 2 3

rotate(rotate(rotate(rotate(x))))
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9

90 degree counter clockwise rotation of R matrix:

Doing the transpose prior to the reverse is the same as rotate counter clockwise:

foo = matrix(1:9, 3)
foo
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9

foo <- apply(t(foo),2,rev)
foo

## [,1] [,2] [,3]
## [1,] 7 8 9
## [2,] 4 5 6
## [3,] 1 2 3

R image() plots matrix rotated?

You could reverse the matrix, then transpose.

mat1 <- apply(mat1, 2, rev)
image(1:3, 1:3, t(mat1))

It's confusing because it draws by row from bottom up, but R indexes matrices by column, top down. So, the pixels in the first row, from left to right, correspond to the first column in the matrix, top down.

Rotate NxN Matrix Counter(anti)-Clockwise 90 Degress

If you reverse the order of each individual row and then taken rows in opposite order from a clockwise rotation, you get a count-clockwise rotation.

A B C                  G D A               A D G                  C F I
D E F -> Clockwise -> H E B -> Reverse -> B E H -> Opposite -> B E H
G H I I F C Rows C F I Ordering A D G

Matrix Counter
Clockwise

Usually it's easier (and more computationally efficient) to do a clockwise rotation rotation on the original matrix in reverse order if you already have a clockwise rotating algorithm available.

1 2 3                9 8 7                 3 6 9
4 5 6 -> Reverse -> 6 5 4 -> Clockwise -> 2 5 8
7 8 9 Indices 3 2 1 1 4 7

Matrix Counter
Clockwise

You can also just take 3 clockwise rotations to get to a counter clockwise rotation.

Though in reality it's usually fairly easy to edit the clockwise algorithm to your purposes directly. So I'd only use the above options if you don't care about efficiency and don't want to work through the logic of changing the direction of rotation.

Rotate a matrix in Prolog

What you want is not quite matrix transposition... but almost!


:- use_module(library(clpfd)).

matrix_rotated(Xss, Zss) :-
transpose(Xss, Yss),
maplist(reverse, Yss, Zss).

Sample query:


?- matrix_rotated([[a1,a2,a3,a4],
[b1,b2,b3,b4],
[c1,c2,c3,c4]], Xss).
Xss = [[c1,b1,a1],
[c2,b2,a2],
[c3,b3,a3],
[c4,b4,a4]].

Rotating a matrix in a non standard 2d plane

The clockwise rather than anti-clockwise just means flipping the sign on the angle.

To get the full result, we just transform into 'standard coords', do the rotation, and transform back:

The coordinate transform and its inverse is:

(x') = ( 1  0 ) (x)
(y') ( 0 -1 ) (y)

A rotation anti-clockwise is:

(x') = (  cos(angle) -sin(angle) ) (x)
(y') ( sin(angle) cos(angle) ) (y)

So a rotation clockwise is:

(x') = (  cos(angle)  sin(angle) ) (x)
(y') ( -sin(angle) cos(angle) ) (y)

Altogether this gives:

(x') = ( 1  0 )(  cos(angle)  sin(angle) ) ( 1  0 )(x)
(y') ( 0 -1 )( -sin(angle) cos(angle) ) ( 0 -1 )(y)

Multiply the matrices to get:

(x') = (  cos(angle)  sin(angle) ) (x)
(y') ( -sin(angle) cos(angle) ) (y)

Now, as you may by now have realized, this is actually the same matrix as rotating 'standard coords' in the anti-clockwise direction.

Or in code:

// Angle in radians
double x2 = cos(angle) * x1 - sin(angle) * y1;
double y2 = sin(angle) * x1 + cos(angle) * y1;

For example, if angle is 180 degrees, cos(angle) is -1, and sin is 0, giving:

double x2 =  -x1;
double y2 = -y1;

Load and display an image: why is it rotated 90°?

There may be a better way, but this works - transpose and then flip the columns:

image(t(img)[,ncol(img):1],axes=FALSE,useRaster=TRUE)

The reason for this flipping is the difference between image contexts and tabular contexts: from ?image,

Notice that ‘image’ interprets the ‘z’ matrix as a table of
‘f(x[i], y[j])’ values, so that the x axis corresponds to row
number and the y axis to column number, with column 1 at the
bottom, i.e. a 90 degree counter-clockwise rotation of the
conventional printed layout of a matrix.

How to rotate a N x N matrix by 90 degrees?

for(int i=0; i<n/2; i++)
for(int j=0; j<(n+1)/2; j++)
cyclic_roll(m[i][j], m[n-1-j][i], m[n-1-i][n-1-j], m[j][n-1-i]);


void cyclic_roll(int &a, int &b, int &c, int &d)
{
int temp = a;
a = b;
b = c;
c = d;
d = temp;
}

Note I haven't tested this, just compoosed now on the spot. Please test before doing anything with it.



Related Topics



Leave a reply



Submit