Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问numpy数组中索引满足某些条件的所有元素?_Python_Numpy_Scipy - Fatal编程技术网

Python 如何访问numpy数组中索引满足某些条件的所有元素?

Python 如何访问numpy数组中索引满足某些条件的所有元素?,python,numpy,scipy,Python,Numpy,Scipy,例如,我想将一个矩阵在其反对角线上的所有元素设置为零(I+j>>n=4 >>>idx=np.arange(n) >>>idx[:,无]+idx 数组([[0,1,2,3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]) >>>idx[:,无]+idx>>mat[idx[:,None]+idx>>垫子 数组([[0,0,0,3], [ 0, 0, 6, 7], [ 0, 9, 10, 11], [12, 13, 14, 15]]) 由于您的矩阵看起来

例如,我想将一个矩阵在其反对角线上的所有元素设置为零(I+j 我曾想过生成一个掩码,但它会导致访问掩码矩阵中的这些元素的相同问题


最好的解决方案是什么?

由于您的矩阵似乎是正方形,您可以使用布尔掩码并执行以下操作:

n = mat.shape[0]
idx = np.arange(n)
mask = idx[:, None] + idx < n - 1
mat[mask] = 0
n=mat.shape[0]
idx=np.arange(n)
掩码=idx[:,无]+idx
要了解正在发生的事情:

>>> mat = np.arange(16).reshape(4, 4)
>>> n = 4
>>> idx = np.arange(n)
>>> idx[:, None] + idx
array([[0, 1, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6]])
>>> idx[:, None] + idx < n - 1
array([[ True,  True,  True, False],
       [ True,  True, False, False],
       [ True, False, False, False],
       [False, False, False, False]], dtype=bool)
>>> mat[idx[:, None] + idx < n  -1] = 0
>>> mat
array([[ 0,  0,  0,  3],
       [ 0,  0,  6,  7],
       [ 0,  9, 10, 11],
       [12, 13, 14, 15]])
>mat=np.arange(16).重塑(4,4)
>>>n=4
>>>idx=np.arange(n)
>>>idx[:,无]+idx
数组([[0,1,2,3],
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
>>>idx[:,无]+idx>>mat[idx[:,None]+idx>>垫子
数组([[0,0,0,3],
[ 0,  0,  6,  7],
[ 0,  9, 10, 11],
[12, 13, 14, 15]])

由于您的矩阵看起来是方形的,您可以使用布尔掩码并执行以下操作:

n = mat.shape[0]
idx = np.arange(n)
mask = idx[:, None] + idx < n - 1
mat[mask] = 0
n=mat.shape[0]
idx=np.arange(n)
掩码=idx[:,无]+idx
要了解正在发生的事情:

>>> mat = np.arange(16).reshape(4, 4)
>>> n = 4
>>> idx = np.arange(n)
>>> idx[:, None] + idx
array([[0, 1, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6]])
>>> idx[:, None] + idx < n - 1
array([[ True,  True,  True, False],
       [ True,  True, False, False],
       [ True, False, False, False],
       [False, False, False, False]], dtype=bool)
>>> mat[idx[:, None] + idx < n  -1] = 0
>>> mat
array([[ 0,  0,  0,  3],
       [ 0,  0,  6,  7],
       [ 0,  9, 10, 11],
       [12, 13, 14, 15]])
>mat=np.arange(16).重塑(4,4)
>>>n=4
>>>idx=np.arange(n)
>>>idx[:,无]+idx
数组([[0,1,2,3],
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
>>>idx[:,无]+idx>>mat[idx[:,None]+idx>>垫子
数组([[0,0,0,3],
[ 0,  0,  6,  7],
[ 0,  9, 10, 11],
[12, 13, 14, 15]])