查找3D numpy数组的y坐标的最大值和最小值

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

我是Python的完整入门者,正在尝试使用它来生成用于分子模拟的合并数据文件。

我正在尝试使用ASE python代码将一组原子沿y轴放置在另一组原子上。为此,我需要找到存储为3D numpy数组(atoms_1)的一个原子组的y坐标的最小值和存储为3D numpy数组(atoms_2)的第二个原子组的y坐标的最大值。哪种方法最好呢?

我尝试过:

y_min = np.max([atoms_2[:,1]])

y_max = np.min([atoms_1[:,1]])

不确定语法是否正确。任何帮助或提示表示赞赏。

python arrays numpy max min
3个回答
2
投票

您正在做什么似乎是正确的。您也可以删除括号:

y_min = np.max(atoms_2[:,1])

y_max = np.min(atoms_1[:,1])

np.max是注释中建议的np.amax的别名。


0
投票

您可以使用以下方法在每列中获得最大值:

import numpy as np

atoms = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9],
                  [1, 4, 6]])

print(np.amax(atoms, axis=0))

>>> [7 8 9]
print(np.amin(atoms, axis=0))
>>> [1 2 3]

要分配它,只需使用:

x_max, y_max, z_max = np.amax(atoms, axis=0)
x_min, y_min, z_min = np.amin(atoms, axis=0)

0
投票

您也可以将轴传递到maxmin

a = np.arange(27).reshape(3,3,-1)

a.max(axis=1), a.min(axis=1)

输出:

(array([[ 6,  7,  8],
        [15, 16, 17],
        [24, 25, 26]]),
 array([[ 0,  1,  2],
        [ 9, 10, 11],
        [18, 19, 20]]))
© www.soinside.com 2019 - 2024. All rights reserved.