简体   繁体   English

检查矩阵/数组的索引,然后检查某些条件

[英]check on indices of matrix/array and then check for some condition

This code converts a square matrix to strict lower triangular matrix (lower elements to 0) 此代码将方矩阵转换为严格的下三角矩阵(下元素为0)

a=np.random.randn(9).reshape((3,3))

a
Out[61]: 
array([[-0.18314209,  0.3710528 , -1.46067261],
       [-0.55834476, -1.41924213, -0.04127718],
       [ 0.40134248, -0.41759044,  1.83573994]])

def subs_tri_0(mat,i,j): mat[i,j] = 0

[subs_tri_0(a,i,j) for i,j in product(xrange(a.shape[0]),xrange(a.shape[1])) if i > j]
Out[63]: [None, None, None]

a
Out[64]: 
array([[-0.18314209,  0.3710528 , -1.46067261],
       [ 0.        , -1.41924213, -0.04127718],
       [ 0.        ,  0.        ,  1.83573994]])

Is there a way to this using short and sweet where? 有没有办法在哪里使用简短而甜蜜的方法呢?

It doesn't use numpy.where , but you could use numpy.tril_indices to set the lower triangular to zero: 它不使用numpy.where ,但是您可以使用numpy.tril_indices将下部三角形设置为零:

>>> a
array([[ 0.05559341, -1.93583316, -1.19666435],
       [-0.33450047,  0.63275874,  0.77152195],
       [-0.73106122, -1.57602057,  0.41878224]])
>>> a[np.tril_indices(3, k=-1)] = 0
>>> a
array([[ 0.05559341, -1.93583316, -1.19666435],
       [ 0.        ,  0.63275874,  0.77152195],
       [ 0.        ,  0.        ,  0.41878224]])

Note that you need to pass k=-1 to numpy.tril_indices to not include the diagonal. 请注意,您需要将k=-1传递给numpy.tril_indices以不包括对角线。

Found the result. 找到了结果。 Although I'm gona go ahead with @mdml solution. 尽管我要使用@mdml解决方案。 which is to the point btw. 顺便说一句

This is just a positive side effect of looping through indices to check for row,col conditions. 这只是循环遍历索引以检查行,列条件的积极副作用。 perhaps anyone could use it for some other meaningful purpose where tril and triu methods are not the case. 也许任何人都可以将其用于其他有意义的目的,而tril和triu方法不是这种情况。

a=np.random.randn(9).reshape((3,3))

r,c = where(a)
where((r>c).reshape(a.shape),0,a)
Out[169]: 
array([[ 1.49230558,  0.6321149 ,  0.05299907],
       [ 0.        ,  0.14736346,  0.42516369],
       [ 0.        ,  0.        , -0.6878655 ]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查矩阵中的某些元素是否具有内聚性 - Check if some elements in a matrix are cohesive 检查数组中的条件 - Check for a condition within an array 循环嵌套列表(二维矩阵)并运行条件检查以检查在第二个列表中指定的特定索引的值 - Looping over a nested list (2d Matrix) and running a condition check for the values of specific indices that are designated within a second list 如果分配了变量,则检查python中的某些条件 - if variable is assigned then check some condition in python Python:检查条件并打印数组的行 - Python: check a condition and print the row of an array 如何使用带有条件的断言来检查 Python 中矩阵中的每个元素? - How to use assert with condition to check on every element in a matrix in Python? 检查矩阵中的值是否与数组中的值匹配,否则返回矩阵索引 - Check if values in matrix matches with values from array and return the matrix index if not 如何检查矩阵的某些元素并相应地生成一组矩阵? - How to check some elements of a matrix and generate a set of matrices accordingly? 如何检查列表(或数组)上的条件,然后在Python中根据该条件对元素进行排序 - How to check a condition on a list (or array) and then sort the elements based on that condition in Python 在 Python 中获取满足条件的矩阵的行索引 - Obtaining row indices of a matrix that satisfies a condition in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM