- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 bool 数组 A 和 B,想得到 C。C 就像 A 和 B 的逻辑与,但在匹配索引时有一些回旋余地。也就是说,逻辑 AND 将执行 A[r, c] AND B[r, c],但我想要的是 A[r +/- 1, c +/- 1] AND B[r +/- 1 , c +/- 1]。有没有一种好的方法可以做到这一点,最好不要遍历每个索引?
>>> import numpy as np
>>> A
np.ndarray([[True, False, False, True],
[False, False, False, False],
[False, False, True, False],
[False, False, False, False])
>>> B
np.ndarray([[False, True, False, False],
[False, False, False, False],
[True, True, True, False],
[False, False, False, False])
>>> np.logical_and(A, B) # only (2,2) is True
np.ndarray([[False, False, False, False],
[False, False, False, False],
[False, False, True, False],
[False, False, False, False])
>>> C # (0,0), (1,0), and (2,1) also become True
np.ndarray([[True, False, False, False],
[True, False, False, False],
[False, True, True, False],
[False, False, False, False])
最佳答案
如果我理解正确,A[r +/- 1, c +/- 1] AND B[r +/- 1, c +/- 1] 将产生以下结果,正如@AetherUnbound 所复制的那样。
[[ True True True False]
[ True True True True]
[False True True True]
[False True True True]]
如果这是所需的输出,我们可以对 a 和 b 使用卷积运算,最后进行逻辑与。
解决方案
from scipy import ndimage
#define a convolution filter with size 3*3
f = np.full((3,3),True, dtype=bool)
#Convolve A and B using a 3*3 filter and then do a logical and in the end.
np.logical_and(ndimage.convolve(A,f,mode='constant', cval=False),ndimage.convolve(B,f,mode='constant', cval=False))
Out[766]:
array([[ True, True, True, False],
[ True, True, True, True],
[False, True, True, True],
[False, True, True, True]], dtype=bool)
关于python - Numpy 逻辑和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43903626/
我有 bool 数组 A 和 B,想得到 C。C 就像 A 和 B 的逻辑与,但在匹配索引时有一些回旋余地。也就是说,逻辑 AND 将执行 A[r, c] AND B[r, c],但我想要的是 A[r
我的代码中的一些断言刚开始表现得很奇怪,我想知道以前是否有人遇到过类似情况。一个简短的代码片段,如: #include class A{ protected: bool is
我希望有人可以帮助我解决以下问题,答案是最好的,但如果您能指出我正确的方向,这也会有所帮助。 我是大学最后一年的学生,这些问题来自之前关于形式方法的考试,我可以知道为今年的论文准备的答案。我们的讲师似
假设我需要为某些输入构建一个真值表,它要求我提供逻辑和、算术和和逻辑乘积。它们之间有什么区别? 最佳答案 逻辑和 - 一种计算机加法,当一个或两个输入变量为 1 时,结果为 1;当输入变量均为 0 时
我有一个过滤器,在我的例子中它是SessionFilter,它在 doFilter() 方法中有一些自定义逻辑来验证请求,在验证此请求的过程中我正在执行数据库操作。当我部署在Weblogic环境中时,
这个问题在这里已经有了答案: The output of cout << 1 && 0; (1 个回答) 关闭 3 年前。 我发现 (true && false) 给出 0,而 true && fa
C语言真/假表 我听说过C语言的表true false for and && or ||是一种数学,他们说如果 true+true=true 和 false+true=false 我对此有点困惑,我尝
我在查询中遇到 OR 逻辑问题。我正在尝试对列 (D) 求和,如果: • A 列 = cars/build-price/carselector.html • B 列 = 包含 ateca 或 ibiz
我有一个带有图像组件的表单:Image 我想获得该图像中的真实鼠标位置。 为此,我使用下面的公式,该公式位于函数中 mouseXInImage = MouseXInComponent / Compon
在 Javascript 中,我有一个 if 语句失败,但应该通过,但我不明白为什么。这是 if 的结构。 if(parseInt(obj.OptionCredit) > parseInt(Accou
我正在为应用程序问题编写一些伪代码。我想就我是否走在正确的轨道上,或者我的做法是否完全错误,提出一些建议? 我有 3 个查询条目值: 申请人年龄 配偶年龄 child 数量 对于上述每个值,我的费率表
我的输出是正确的,但我想知道 STUFF 的实际工作原理。 我有一个简单的查询,它返回 @startDate 和 @endDate 之间的总月数。 在 STUFF 的帮助下,我将那几个月存储到 @co
分离 express 应用程序的不同层的最佳方法是什么,这样我的 app.js 文件就不会塞满各种功能?我来自 Java 世界,所以我通常将模型、业务逻辑和 DAO 代码放在不同的层中。 另一个困扰我
我是一名优秀的程序员,十分优秀!