我不得不以下索引的集合使用ndarray.flatten()蟒若干陣列的陣列,Python的
ind = array([[array([0, 1, 3, 4])],
[array([0, 1, 2, 3, 4, 5])],
[array([1, 2, 4, 5])],
[array([0, 1, 3, 4, 6, 7])],
[array([0, 1, 2, 3, 4, 5, 6, 7, 8])],
[array([1, 2, 4, 5, 7, 8])],
[array([3, 4, 6, 7])],
[array([3, 4, 5, 6, 7, 8])],
[array([4, 5, 7, 8])]], dtype=object)
一些數據的
讓說X;
X = array([[ 0. , 0. ],
[ 0.5, 0. ],
[ 1. , 0. ],
[ 0. , 0.5],
[ 0.5, 0.5],
[ 1. , 0.5],
[ 0. , 1. ],
[ 0.5, 1. ],
[ 1. , 1. ]])
有沒有一種方法,我可以替換以下for循環一些內置的功能在Python;
A_flat = ind.flatten()
for i in range(N):
print X[i,0] - X[A_flat[i],0]
> [ 0. -0.5 0. -0.5]
[ 0.5 0. -0.5 0.5 0. -0.5]
[ 0.5 0. 0.5 0. ]
[ 0. -0.5 0. -0.5 0. -0.5]
[ 0.5 0. -0.5 0.5 0. -0.5 0.5 0. -0.5]
[ 0.5 0. 0.5 0. 0.5 0. ]
[ 0. -0.5 0. -0.5]
[ 0.5 0. -0.5 0.5 0. -0.5]
[ 0.5 0. 0.5 0. ]
並存儲結果到一個9×9零矩陣(或稀疏矩陣),以獲得讓說乙與正確索引;
什麼是'N'繼續應該是? – chrisaycock
@chrisaycock 'N = len(X [:,0])',這裏'N'是'9'。 – madf19