This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

I need some type of standard way of rotating weird sized matrices, like 2x4 (or 4x2, im never quite certain if m = rows or columns when dealing with an mXn, and this is even after Linear Algebra..sigh)

Example:

[ 1 1 1 1 ]

[ 1 0 0 0 ]

Rotating this 90 degrees, I would like to have an output of

[ 1 1 ]

[ 0 1 ]

[ 0 1 ]

[ 0 1 ]

Another example,

[ 1 1 ]

[ 1 0 ]

rotated -90

[ 1 0 ]

[ 1 1 ]

This application is being done in Java, and I'm using a double[][] array.

Many thanks

Example:

[ 1 1 1 1 ]

[ 1 0 0 0 ]

Rotating this 90 degrees, I would like to have an output of

[ 1 1 ]

[ 0 1 ]

[ 0 1 ]

[ 0 1 ]

Another example,

[ 1 1 ]

[ 1 0 ]

rotated -90

[ 1 0 ]

[ 1 1 ]

This application is being done in Java, and I'm using a double[][] array.

Many thanks

Is there not some form of cos/sin combination of a matrix I could use to multiple the current matrix by?

ie something like this works for rotating on the z axis

[ cos(x) -sin(x) 0 ]

[ sin(x) cos(x) 0 ]

[ 0 0 1 ]

I just don't know how I can apply the theory behind this matrix to matrices of indefinite sizes. I will try your solution, although I would truly like to have a Java Matrix class that has rotate(degrees) and doesn't do something arbitrary to -only- allow rotations of 90/-90 increments.

[CEHJ: You're in the Java TA ...]

What's that? Topic Announcement??

[ TA = Topic Area. Your title had JavaScript and C++ but the topic itself was posted in Java. ]

Doesn't the same also apply to my matrix? Isn't an image just thousands of pixels (RGB) located in an array, and rotation on those changes the indices of the pixels and their color values.

ie one pixel could be something like

pixels[0][0] = 255; // R

pixels[0][1] = 128; // G

pixels[0][2] = 200; // B

private static int[][] rotateLeft(int[][] matrix){

int[][] result=new int[matrix[0].length][matr

for(int i=matrix[0].length-1, j=0; i>=0; i--, j++){

for(int k=0; k<matrix.length; k++){

result[j][k]=matrix[k][i];

}

}

return result;

}

private static int[][] rotateRight(int[][] matrix){

int[][] result=new int[matrix[0].length][matr

for(int i=0; i<matrix[0].length; i++){

for(int j=0, k=matrix.length-1; k>=0; j++,k--){

result[i][j]=matrix[k][i];

}

}

return result;

}

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

There are a few ways of approaching matrix rotation. One way is as follows:

- for rotation by -90, reverse the order of each element in each row ("mirror" the matrix) then transpose

- for rotation by +90, swap the order of the rows ("flip" the matrix) then transpose