python numpy如何沿中心旋转3D阵列

发布于 2025-02-08 00:55:23 字数 491 浏览 2 评论 0 原文

我需要转换以下3D数组。 我需要第1列才能成为1'strow, 2'nd列成为2'第2行,而第3列成为第3行。

 3  4  5       4  7  3
 7  1  2   =>  5  1  4
 4  5  6       6  2  5  

 6  7b 1       7b 3 6 
 3  4  5   =>  1  4 7b
 7b 1  2       2  5 1 

这项工作 print(np.rot90(r [6],3)) 在2D阵列上,我需要在3D阵列中 我需要它为此数组工作

[[['6' '7b' '1']
  ['3' '4' '5']
  ['7b' '1' '2']]

 [['3' '4' '5']
  ['7' '1' '2']
  ['4' '5' '6']]

 [['7' '1' '2']
  ['4s' '5' '6']
  ['1' '2' '3']]]

I need to transform the following 3D array.
I need the 1'st column to become the 1'strow,
2'nd column to become 2'nd row, and 3'rd column to become 3'rd row.

 3  4  5       4  7  3
 7  1  2   =>  5  1  4
 4  5  6       6  2  5  

 6  7b 1       7b 3 6 
 3  4  5   =>  1  4 7b
 7b 1  2       2  5 1 

this work print(np.rot90(r[6], 3)) but only
on 2d array, and I need it in a 3d array
I need it to work for this array

[[['6' '7b' '1']
  ['3' '4' '5']
  ['7b' '1' '2']]

 [['3' '4' '5']
  ['7' '1' '2']
  ['4' '5' '6']]

 [['7' '1' '2']
  ['4s' '5' '6']
  ['1' '2' '3']]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-02-15 00:55:23

似乎 使用 内部,因此我们可以使用 np.transpose 直接用于您的情况。 iiuc,您可以将阵列转换为第二个&第三维,然后根据需要对其进行反转:

a.transpose([0, 2, 1])[:, :, ::-1]

# [[['7b' '3' '6']
#   ['1' '4' '7b']
#   ['2' '5' '1']]
# 
#  [['4' '7' '3']
#   ['5' '1' '4']
#   ['6' '2' '5']]
# 
#  [['1' '4s' '7']
#   ['2' '5' '1']
#   ['3' '6' '2']]]

它将获得与 np.Rot90 相同的结果,该结果由 @szczesny在评论中

It seems np.rot90 use np.transpose internally, so we can use np.transpose instead that directly for your case. IIUC, you can transpose the array for 2nd & 3rd dimensions and then reverse them as needed:

a.transpose([0, 2, 1])[:, :, ::-1]

# [[['7b' '3' '6']
#   ['1' '4' '7b']
#   ['2' '5' '1']]
# 
#  [['4' '7' '3']
#   ['5' '1' '4']
#   ['6' '2' '5']]
# 
#  [['1' '4s' '7']
#   ['2' '5' '1']
#   ['3' '6' '2']]]

It will get the same results as np.rot90 that well explained by @Szczesny in the comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文