Rotate 2D axis around the central x axis
Question:
I have a simple 2D array:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
And would like it to be:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
How could this be done, and then, in general, how could this be done for an [n,m] sort of array?
Answers:
Code:
a = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
b = a.copy()
for i in range(len(a)):
b[i], b[len(a) - i - 1] = a[len(a) - i - 1], a[i]
print(b)
Output:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
I don’t know if there is a method for it in Numpy but one way is to re-index it backwards.
This is how it’s done:
import numpy as np
a = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
a = a[list(range(len(a)-1,-1,-1))]
a
output:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
You can use np.flipud
(up/down) or with similar result np.flip
on the axis=0
:
import numpy as np
arr = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
flipud_arr = np.flipud(arr)
print(flipud_arr)
flip_arr = np.flip(arr, axis=0)
print(flip_arr)
Both have the same output:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
Both work in general, so also for even rows:
arr = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[ 3, 0, 8, 1]])
np.flipupd(arr)
Output:
array([[ 3, 0, 8, 1],
[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
np.flip does the job.
>>>A = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>>np.flip(A, axis=0)
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
Slicing:
>>> ar = np.array([[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [ 9, 10, 11, 12]])
>>> ar[::-1]
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
This returns an array view, which shares memory with the original array. If you do not want to share memory, copy it:
>>> ar[::-1].copy()
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
I have a simple 2D array:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
And would like it to be:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
How could this be done, and then, in general, how could this be done for an [n,m] sort of array?
Code:
a = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
b = a.copy()
for i in range(len(a)):
b[i], b[len(a) - i - 1] = a[len(a) - i - 1], a[i]
print(b)
Output:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
I don’t know if there is a method for it in Numpy but one way is to re-index it backwards.
This is how it’s done:
import numpy as np
a = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
a = a[list(range(len(a)-1,-1,-1))]
a
output:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
You can use np.flipud
(up/down) or with similar result np.flip
on the axis=0
:
import numpy as np
arr = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
flipud_arr = np.flipud(arr)
print(flipud_arr)
flip_arr = np.flip(arr, axis=0)
print(flip_arr)
Both have the same output:
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
Both work in general, so also for even rows:
arr = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[ 3, 0, 8, 1]])
np.flipupd(arr)
Output:
array([[ 3, 0, 8, 1],
[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
np.flip does the job.
>>>A = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>>np.flip(A, axis=0)
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
Slicing:
>>> ar = np.array([[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [ 9, 10, 11, 12]])
>>> ar[::-1]
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
This returns an array view, which shares memory with the original array. If you do not want to share memory, copy it:
>>> ar[::-1].copy()
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])