In Python, how do I rotate a matrix 90 degrees counterclockwise?
up vote
0
down vote
favorite
>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""
Edit: Preferably without numpy.
python
add a comment |
up vote
0
down vote
favorite
>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""
Edit: Preferably without numpy.
python
I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46
I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""
Edit: Preferably without numpy.
python
>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""
Edit: Preferably without numpy.
python
python
edited Nov 11 at 21:22
Yvette Colomb♦
19.9k1369107
19.9k1369107
asked Nov 11 at 16:34
user10634718
141
141
I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46
I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13
add a comment |
I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46
I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13
I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53
I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46
I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13
I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You could use the numpy function rot90
import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])
def rotate_matrix(mat):
return np.rot90(mat)
add a comment |
up vote
1
down vote
Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
If you want to do this in a function, then
def rotate_matrix( m ):
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
and either way, the result for
m = [ [1,2,3], [2,3,3], [5,4,3]]
is
[[3, 3, 3], [2, 3, 4], [1, 2, 5]]
Aside, if you want the usual transpose, then the simple one line pure python version is
[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You could use the numpy function rot90
import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])
def rotate_matrix(mat):
return np.rot90(mat)
add a comment |
up vote
2
down vote
You could use the numpy function rot90
import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])
def rotate_matrix(mat):
return np.rot90(mat)
add a comment |
up vote
2
down vote
up vote
2
down vote
You could use the numpy function rot90
import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])
def rotate_matrix(mat):
return np.rot90(mat)
You could use the numpy function rot90
import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])
def rotate_matrix(mat):
return np.rot90(mat)
answered Nov 11 at 16:40
Vikhyat Agarwal
420214
420214
add a comment |
add a comment |
up vote
1
down vote
Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
If you want to do this in a function, then
def rotate_matrix( m ):
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
and either way, the result for
m = [ [1,2,3], [2,3,3], [5,4,3]]
is
[[3, 3, 3], [2, 3, 4], [1, 2, 5]]
Aside, if you want the usual transpose, then the simple one line pure python version is
[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
add a comment |
up vote
1
down vote
Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
If you want to do this in a function, then
def rotate_matrix( m ):
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
and either way, the result for
m = [ [1,2,3], [2,3,3], [5,4,3]]
is
[[3, 3, 3], [2, 3, 4], [1, 2, 5]]
Aside, if you want the usual transpose, then the simple one line pure python version is
[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
add a comment |
up vote
1
down vote
up vote
1
down vote
Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
If you want to do this in a function, then
def rotate_matrix( m ):
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
and either way, the result for
m = [ [1,2,3], [2,3,3], [5,4,3]]
is
[[3, 3, 3], [2, 3, 4], [1, 2, 5]]
Aside, if you want the usual transpose, then the simple one line pure python version is
[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
If you want to do this in a function, then
def rotate_matrix( m ):
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
and either way, the result for
m = [ [1,2,3], [2,3,3], [5,4,3]]
is
[[3, 3, 3], [2, 3, 4], [1, 2, 5]]
Aside, if you want the usual transpose, then the simple one line pure python version is
[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
edited Nov 12 at 13:01
answered Nov 11 at 16:55
DrM
944314
944314
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
add a comment |
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:45
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250821%2fin-python-how-do-i-rotate-a-matrix-90-degrees-counterclockwise%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53
I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46
I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13