下面的代码沿着与np.diagonal
通常返回的对角线正交的对角线进行迭代。它从位置(0,0)开始,并向右下坐标移动。
代码按预期工作,但所有循环都不是很numpy,并且必须创建许多数组才能做到这一点。
所以我想知道是否有更好的方法来做到这一点,因为我不知道如何跨越我的数组或使用numpy的对角线方法来更好地做到这一点(尽管我希望有一些技巧我看不到)。
import numpy as np
A = np.zeros((4,5))
#Construct a distance array of same size that uses (0, 0) as origo
#and evaluates distances along first and second dimensions slightly
#differently so that no values in the array is the same
D = np.zeros(A.shape)
for i in range(D.shape[0]):
for j in range(D.shape[1]):
D[i, j] = i * (1 + 1.0 / (grid_shape[0] + 1)) + j
print D
#[[ 0. 1. 2. 3. 4. ]
# [ 1.05882353 2.05882353 3.05882353 4.05882353 5.05882353]
# [ 2.11764706 3.11764706 4.11764706 5.11764706 6.11764706]
# [ 3.17647059 4.17647059 5.17647059 6.17647059 7.17647059]]
#Make a flat sorted copy
rD = D.ravel().copy()
rD.sort()
#Just to show how it works, assigning incrementing values
#iterating along the 'orthagonal' diagonals starting at (0, 0) position
for i, v in enumerate(rD):
A[D == v] = i
print A
#[[ 0 1 3 6 10]
# [ 2 4 7 11 14]
# [ 5 8 12 15 17]
# [ 9 13 16 18 19]]
编辑
为了澄清,我想在整个A
中逐元素迭代,但要按照上面的代码调用的顺序进行(显示在最终的print
中)。
迭代沿着对角线的哪个方向并不重要(如果1和2切换放置,以及3和5切换放置等)。在A
中),只有对角线与A的主对角线正交(由np.diag(A)
产生的对角线)。
这个问题的应用/原因在我之前的问题中(在该问题底部的解决方案部分):Constructing a 2D grid from potentially incomplete list of candidates
4条答案
按热度按时间uplii1fm1#
这里有一个避免Python for循环的方法。
首先,让我们看看我们的加法表:
关键思想是,如果我们按顺序访问加法表中的和,我们将以所需的顺序迭代数组。
我们可以使用
np.argsort
找到与该订单相关的索引:idx
是金色的。它基本上是迭代任何形状(4,5)的2D数组所需的一切,因为2D数组只是重塑的1D数组。如果你的最终目标是生成你在文章末尾显示的数组
A
,那么你可以再次使用argsort
:或者,如果您需要为
A
分配其他值,也许这会更有用:我知道你要求一种方法来 * 迭代 * 你的数组,但我想展示上面的内容,因为通过整个数组赋值或numpy函数调用(如np.argsort)生成数组可能比使用Python循环更快。但是如果你需要使用Python循环,那么:
mqxuamgl2#
只要它不是一个大的矩阵就更简单了。
olqngx593#
我不确定这是不是你真正想要的,但也许:
编辑作为一般的解决方案,对于任意形状的数组,可以使用
例如,给出
t30tvxxf4#
直观的循环
对于那些(像我一样)正在寻找直观的嵌套for循环的人。
**注意:**计算效率低下!