Wrestling Matrix Transformations
March 16, 2025 | Categories: programming algorithmsI can grapple with Binary Tree’s and find Islands on a Graph, but a challenge for me was always been dealing with Transformations. I think it’s the tracking of elements and realizing how you’re swapping indices for other indices in a proper pattern without going out of bounds of the arrays. One common challenge has been rotating a Matrix by 90 degrees.
- Transpose the Matrix:
- Swap matrix[i][j] with matrix[j][i] for all i < j. This swaps rows and columns, effectively flipping the matrix over it’s main diagonal.
- Reverse each row:
- Since transposing alone results in a counter-clockwise transformation, reversing each row converts it into a clockwise 90-degree rotation.
As always, once you see the code it looks easy as can be. It's just wrapping your mind around the actual transformation and how you'll get it to work how you're wanting with element positioning.