arrays - Numpy:在其他布尔数组的“真”上覆盖布尔数组
问题描述
我有一个 bool 2D 数组 A,其中 True 的数字是 bool 2D 数组 B 的维数。
A = np.array([[False, True, True, False, True],[False, False, False, False, False],[False, True, True, False, True]])
B = np.array([[True, False, True],[True, True, True]])
A =[[False, True, True, False, True],
[False, False, False, False, False],
[False, True, True, False, True]]
B =[[True, False, True],
[True, False, True]]
我想在 A 的“真”阵列上“覆盖”B,这样我就可以得到
C =
[[False, **True**, **False**, False, **True**],
[False, False, False, False, False],
[False, **True**, **False**, False, **True**]]
我的最终目标是操作数组
arr = [[1, 2 , 3 , 4, 5 ], [6,7,8,9,10], [11, 12 , 13 , 14, 15 ]]
有类似的东西
arr[A] = arr[A] + B*2
要得到
arr = [[1, 4 , 3 , 4, 7 ], [6,7,8,9,10], [11, 14 , 13 , 14, 17 ]]
提前致谢。
解决方案
# get the indexes that are True
Xi = np.nonzero(A)
# convert to an array of 1D
B1 = np.ndarray.flatten(B)
# use Xi for dynamic indexing
A[Xi]=B1
推荐阅读
- java - 如何更改查找共享库的位置?
- javascript - 在正则表达式中结合 2 个条件
- c# - C# winform 复选框仍然选中
- database - 与 MongoDB 的 BSON 更新相比,Postgres jsonb_set 的性能如何?
- javascript - 上下文/弹出菜单在黄金布局的拆分器上被剪切
- flutter - 下载 ZIP,解压缩并显示图像文件,但未从文档目录加载?
- node.js - 如何更改 discord.js 中的角色设置?
- c++ - 将 cv::Mat 插入 std::map 后会改变吗?根本原因是什么?
- typescript - TypeScript:从 Record<> 接口继承值的类型,而不是所有的键
- php - PHP 致命错误:未捕获的错误:找不到类 'Illuminate\Foundation\Application'