我有以下矩阵:
a = array([
[100. , 100., 100.],
[175.2, 198., 32.],
[ 38. , 82. , 38.],
[155. , 32. , 23.],
[ 38. , 67. , 30.]])
如何更改所有行中的数字,但如果行中的数字不等于38和32,则将最后一行除外的数字改为零?我想得到的是:
a = array([
[ 0 , 0 , 0 ],
[ 0 , 0 , 32.],
[38., 0 , 38.],
[ 0 , 32., 0 ],
[38., 67., 30.]])
然后我想留下最早出现在每一列的数字像这样的:
a = array([
[ 0 , 0 , 0 ],
[ 0 , 0 , 32.],
[38., 0 , 0 ],
[ 0 , 32., 0 ],
[ 0 , 0 , 0 ]])
最佳答案
这应该做到:
positions = [(y, x) for x, y in enumerate(np.argmax(np.isin(a, [32,38]), axis=0))]
result = np.zeros(a.shape)
for p in positions:
result[p] = a[p]
#[[ 0 , 0 , 0 ],
# [ 0 , 0 , 32.],
# [38., 0 , 0 ],
# [ 0 , 32., 0 ],
# [ 0 , 0 , 0 ]]