Anticlockwise rotation of matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • linuxx
    New Member
    • Feb 2008
    • 6

    Anticlockwise rotation of matrix

    Hello Guys,

    If I rotate 2D array anticlockwise,h ow would I map original variables into new matrix?

    Consider the example:
    original matrix =

    1 2 3
    4 5 6
    7 8 9

    Final matrix =

    3 6 9
    2 5 8
    1 4 7

    Now If I want to access the 6 from original matrix.I'd use..
    i = 1 and j =2..i.e. OriginalArr[i][j] will give me 6.

    My question is, how could I access same number(here 6) from final matrix using previous i and j ?

    One solution could be FinalnalArr[ i -1 ] [ j -1 ]
    but this is not generalized.

    I want general solution that work for any i,j as well as for any 2D matrix(any dimensions).
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Just look at your matrixes; let their dimension be NxN. Row i in the transformed
    matrix is column N-1-i in the original matrix. Column j in the transformed matrix
    is row j in the original matrix, so A[j][N-i-1] gives you the transformed matrix.

    kind regards,

    Jos

    Comment

    • linuxx
      New Member
      • Feb 2008
      • 6

      #3
      Originally posted by JosAH
      Just look at your matrixes; let their dimension be NxN. Row i in the transformed
      matrix is column N-1-i in the original matrix. Column j in the transformed matrix
      is row j in the original matrix, so A[j][N-i-1] gives you the transformed matrix.

      kind regards,

      Jos

      Hey Jos...
      Don't forget, 2D array can have dimensions as MxN.
      Your solution works for NxN.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by linuxx
        Hey Jos...
        Don't forget, 2D array can have dimensions as MxN.
        Your solution works for NxN.
        It also works for an MxN matrix; check it out.

        kind regards,

        Jos

        Comment

        • linuxx
          New Member
          • Feb 2008
          • 6

          #5
          Originally posted by JosAH
          It also works for an MxN matrix; check it out.

          kind regards,

          Jos
          You've given me the formula...
          A[j][N-i-1]
          I was just wondering about N..should it be number of Rows or number of Columns
          Any way...It's working...

          Will you please explain me..how you figured out this formula?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by linuxx
            You've given me the formula...
            A[j][N-i-1]
            I was just wondering about N..should it be number of Rows or number of Columns
            Any way...It's working...

            Will you please explain me..how you figured out this formula?
            I used a rotation matrix and rotated the indexes (col, row) which gives (-row, col),
            then I fiddled a bit to get those index values back in the range [0, N)

            kind regards,

            Jos

            Comment

            Working...