Rotate a Matrix: With New Array (Python)

Hello everyone. I have another algorithm for you all today. One I’ve been meaning to tackle for a while now. La rotation d’une matrice…Rotation of a Matrix! I have two versions for you all, both in Python. This post makes a copy of the original matrix and adds the correct values to it, the second post (coming soon) mutates the original array in-place. We will be rotating the matrix 90 degrees. Okay, empecemos!

Here is Version 1: The full code

Screenshot 2018-10-17 at 11.36.20 AM

 

The Breakdown

 

Okay, first make sure your input 2d array/matrix is an nxn. The number of rows must equal the number of columns. We define a function, rotate, that holds an argument, matrix. We begin with edge cases. If we are not give any matrix or if the length of our matrix is less than one, we just return and end.

If the length of our matrix is equal to one, this means we only received one row so we just return it as there isn’t anything to rotate.

Screenshot 2018-10-17 at 11.31.39 AM

If we’ve made it this far we have a valid matrix, so let’s do a list comprehension to store it in our new variable, new_matrix.  What you see here is a for loop going one by one into our original matrix and making a copy of it. This copy is stored into new_matrix. 

row_count will hold the number of rows in the matrix. In this nxn we have 3 rows and 3 columns.

Screenshot 2018-10-17 at 11.34.29 AM

Our first for loop, x , starts at 0 and grabs the rows. At first iteration, 0, it grabs the first row. The second for loop, j , grabs our columns. While x = 0, j iterates to j = 0, j = 1, j = 2. After 2 there are no more rows (3 rows total but indexes start at 0), so x increments to 1.

So now for the magic. Note, that when we are rotating a matrix the rows become the columns. The first row, [1,2,3], will now be the last column, and vice versa. I found a great video on Khan Academy that explains this concept of “Transposing”, well. They use mxn matrices in some of the video examples but it’s the same concept.

With new_matrix[j][row_count-1-x] on first iteration we are accessing the values of the last column of the new_matrix and swapping them with the first row values of the original matrix. On first iteration new_matrix[0][2], 2nd new_matrix[1][2], 3rd new_matrix[2][2]. This is why we have [j] first, so that it can iterate down the column and x will always be grabbing from the first row of the original matrix on the first iteration. Try drawing out 2 matrices on paper so you can really understand the magic and follow along. j = columns and x = rows. Please, if you don’t understand ask me below.

Finally, return the new_matrix.

Screenshot 2018-10-17 at 11.34.48 AM

 

Call your function with a matrix you’d like to rotate.

Screenshot 2018-10-17 at 11.39.14 AM

And this should be your result.

Screenshot 2018-10-17 at 1.56.45 PM

 

There you have it. Look out for Version 2 coming soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s