Question

I need an algorithm for (clockwise) rotation of a matrix represented by a one-dimensional array.
So far, I've found the following links but couldn't figure out anything bringing me closer to the solution I need:
http://basgun.wordpress.com/2008/04/11/rotate-array/
http://www.rawkam.com/?p=1008

Any suggestions or clues will be greatly appreciated!

Hristo

Edit: Here is the example
(1 2 3 (one rotation cycle clockwise would make) (7 4 1
 4 5 6                           --------->                             8 5 2
 7 8 9)                                                                    9 6 3)

(7 4 1 (anther rotation cycle clockwise would make) (9 8 7
 8 5 2                          --------->                                  6 5 4
 9 6 3)                                                                        3 2 1)

Was it helpful?

Solution

wikipedia has a great article about "clockwise rotation of matrix" (In-place matrix transposition -> Algorithms)

You didn't say which language you use, but for this case, C-like languages have a great way to access "matrix represented by a one-dimensional array".

OTHER TIPS

Based on this answer, I changed the solution from a 2D array to a 1D array. This is written in C#, but should work for any C-esque language.

public static int[] Rotate1DSquareMatrixClockwise(int[] matrix)
{
    int size = (int)Math.Sqrt(matrix.Length);
    int[] result = new int[matrix.Length];

    for (int i = 0; i < size; ++i)
    {
        for (int j = 0; j < size; ++j)
        {
            result[i * size + j] = matrix[(size - j - 1) * size + i];
        }
    }

    return result;
}

This answer only works for square matrices. Sidenote: To access elements from a square 2D array as a 1D array, it is as simple as: for matrix NxN: m1[i,j] = m2[i * N + j] So, that's all I changed in the linked solution.

It is simple

steps to Rotate Clock wise

1.Take Transpose of matrix

2.Swap Columns

/*a is the given matrix , b is the output matrix ,n is the size of the matrix*/
System.out.println("Transpose of given matrix\n");
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
              b[i][j]=a[j][i];
            }
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(b[i][j]+"\t");
            }
            System.out.println("\n");
        }

        System.out.println("Clockwise Rotation of given matrix\n");

        for(i=0;i<n/2;i++)
        {
            for(j=0;j<n;j++)
            {

                sw=b[j][i];
                b[j][i]=b[j][n-1-i];
                b[j][n-1-i]=sw;
            }
            System.out.println("\n");
        }


        //Print the Result 
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(b[i][j]+"\t");
            }
            System.out.println("\n");
        }

Tested and Worked Well

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top