在Python中操作3D数组

问题描述 投票:0回答:1

我有一个名为 result 的数组,其形状为 (1200,67,67)。沿着长度为 1200 的轴,数组是正确的。然而,另外两个轴已经翻转。我得到:

result[0,:,:] = array([
    [-0.4182856 , -0.12402566, 0.26624826, ..., 0.65988034, 0.7642574, 0.4524528],
    [-0.39774683, -0.18033904, 0.14606595, ..., 0.7325263 , 0.75217724, 0.32839388],
    [-0.34418693, -0.15839723, 0.05166954, ..., 0.70598584, 0.57750726, 0.18543348],
    ...,
    [-0.5998102 , -0.512376 , -0.00168504, ..., 0.08733568, -0.28331557, -0.6596785 ],
    [-0.5967892 , -0.28394988, 0.03711865, ..., -0.03314218, -0.33655524, -0.671829 ],
    [-0.54678684, -0.21562685, 0.01694722, ..., -0.03415907,-0.36989942, -0.66516656]
], dtype=float32)

但我应该得到第二个版本。

result[0,:,:] = array([
    [0.45245281, 0.76425737, 0.65988034, ..., 0.26624826, -0.12402566, -0.41828561 ],
    [0.32839388, 0.75217724, 0.73252630, ..., 0.14606595, -0.18033904, -0.39774683],
    [0.18543348, 0.57750726, 0.70598584, ..., 0.051669542, -0.15839723, -0.34418693],
    ...
    [-0.65967852, -0.28331557, 0.087335683, ..., -0.0016850400, -0.51237601, -0.59981018 ],
    [-0.67182899, -0.33655524, -0.033142179, ..., 0.037118647,  -0.28394988, -0.59678918 ],
    [-0.66516656, -0.36989942, -0.034159068, ..., 0.016947223,  -0.21562685, -0.54678684]
], dtype=float32)

所以我想尝试改变 3d 数组,然后翻转 67 长度的两个轴。谢谢。

python arrays multidimensional-array
1个回答
0
投票

这应该像调换 y 轴和 z 轴一样简单。所以这个问题可以使用

transpose
函数,如下所示:

import numpy as np

# Define the dimensions for the x, y, and z axes
x_dim = 1200
y_dim = 67
z_dim = 67

# Create a NumPy array with the specified dimensions initialized with zeros
original_array = np.zeros((x_dim, y_dim, z_dim))

# Flip the y and z axes using numpy.transpose
flipped_array = np.transpose(original_array, (0, 2, 1))

# Print the shape of the new array to confirm the axes have been flipped
print(flipped_array.shape)

为了示例的目的,我只是显示了一个零的数组形状,但是操作员可以用适当的数字填充它。

© www.soinside.com 2019 - 2024. All rights reserved.