沿着下三角形numpy数组的每一行翻转非零值

时间:2015-11-13 19:27:38

标签: python arrays numpy reverse

我有一个较低的三角形数组,如B:

    if (a <= 0 || b <= 0)
    {
        error("bad values");
    }
    cout << "the area is " << area(a, b)<<'\n';

我想将其翻转为:

B = np.array([[1,0,0,0],[.25,.75,0,0], [.1,.2,.7,0],[.2,.3,.4,.1]])

>>> B
array([[ 1.  ,  0.  ,  0.  ,  0.  ],
       [ 0.25,  0.75,  0.  ,  0.  ],
       [ 0.1 ,  0.2 ,  0.7 ,  0.  ],
       [ 0.2 ,  0.3 ,  0.4 ,  0.1 ]])

也就是说,我想取所有正值,并在正值内反转,留下尾随零。这不是array([[ 1. , 0. , 0. , 0. ], [ 0.75, 0.25, 0. , 0. ], [ 0.7 , 0.2 , 0.1 , 0. ], [ 0.1 , 0.4 , 0.3 , 0.2 ]]) 所做的:

fliplr

任何提示?此外,我正在使用的实际数组类似于>>> np.fliplr(B) array([[ 0. , 0. , 0. , 1. ], [ 0. , 0. , 0.75, 0.25], [ 0. , 0.7 , 0.2 , 0.1 ], [ 0.1 , 0.4 , 0.3 , 0.2 ]]) 而不是B.shape = (200,20,4,4)。每个(4,4)块看起来与上面的示例相同(在200个不同的条目中有不同的数字)。

2 个答案:

答案 0 :(得分:8)

这个怎么样:

# row, column indices of the lower triangle of B
r, c = np.tril_indices_from(B)

# flip the column indices by subtracting them from r, which is equal to the number
# of nonzero elements in each row minus one
B[r, c] = B[r, r - c]

print(repr(B))
# array([[ 1.  ,  0.  ,  0.  ,  0.  ],
#        [ 0.75,  0.25,  0.  ,  0.  ],
#        [ 0.7 ,  0.2 ,  0.1 ,  0.  ],
#        [ 0.1 ,  0.4 ,  0.3 ,  0.2 ]])

相同的方法将推广到由多个下三角形子矩阵组成的任意 N 维数组:

# creates a (200, 20, 4, 4) array consisting of tiled copies of B
B2 = np.tile(B[None, None, ...], (200, 20, 1, 1))

print(repr(B2[100, 10]))
# array([[ 1.  ,  0.  ,  0.  ,  0.  ],
#        [ 0.25,  0.75,  0.  ,  0.  ],
#        [ 0.1 ,  0.2 ,  0.7 ,  0.  ],
#        [ 0.2 ,  0.3 ,  0.4 ,  0.1 ]])

r, c = np.tril_indices_from(B2[0, 0])
B2[:, :, r, c] = B2[:, :, r, r - c]

print(repr(B2[100, 10]))
# array([[ 1.  ,  0.  ,  0.  ,  0.  ],
#        [ 0.75,  0.25,  0.  ,  0.  ],
#        [ 0.7 ,  0.2 ,  0.1 ,  0.  ],
#        [ 0.1 ,  0.4 ,  0.3 ,  0.2 ]])

对于上三角矩阵,您只需从r中减去c,例如:

r, c = np.triu_indices_from(B.T)
B.T[r, c] = B.T[c - r, c]

答案 1 :(得分:4)

以下是2D数组案例的一种方法 -

mask = np.tril(np.ones((4,4),dtype=bool))
out = np.zeros_like(B)
out[mask] = B[:,::-1][mask[:,::-1]]

您可以使用相同的3D掩码将2D数组扩展到最后两个轴的masking,就像这样 -

out = np.zeros_like(B)
out[:,mask] = B[:,:,::-1][:,mask[:,::-1]]

..类似于4D数组的情况,如此 -

out = np.zeros_like(B)
out[:,:,mask] = B[:,:,:,::-1][:,:,mask[:,::-1]]

正如我们所看到的,我们将掩蔽过程保持在(4,4)的最后两个轴上,解决方案基本保持不变。

示例运行 -

In [95]: B
Out[95]: 
array([[ 1.  ,  0.  ,  0.  ,  0.  ],
       [ 0.25,  0.75,  0.  ,  0.  ],
       [ 0.1 ,  0.2 ,  0.7 ,  0.  ],
       [ 0.2 ,  0.3 ,  0.4 ,  0.1 ]])

In [96]: mask = np.tril(np.ones((4,4),dtype=bool))
    ...: out = np.zeros_like(B)
    ...: out[mask] = B[:,::-1][mask[:,::-1]]
    ...: 

In [97]: out
Out[97]: 
array([[ 1.  ,  0.  ,  0.  ,  0.  ],
       [ 0.75,  0.25,  0.  ,  0.  ],
       [ 0.7 ,  0.2 ,  0.1 ,  0.  ],
       [ 0.1 ,  0.4 ,  0.3 ,  0.2 ]])