如何在C ++中使用犰狳在多维数组之间进行按位与运算?

问题描述

我的任务是使用armadillo和C ++在matlab中重写代码is_valid = (DoG == DoG_max) & (DoG >= threshold);DoGDoG_max是具有相同大小907 x 1210 x 5threshold is a scalar的多维数组。

根据 armadillo documentation,内置了按位相等运算符 ==,并且 bit-可以使用the member function .clean()代替明智地大于的操作,该操作用于将除阈值以外的所有元素都设置为零。

这是我的代码:

// arma::cube DoG,DoG_max;  // Size: 907 x 1210 x 5.
arma::ucube is_valid(arma::size(DoG),arma::fill::zeros);
DoG.clean(threshold);
for (int s = 0; s < is_valid.n_slices; ++s) {
  is_valid.slice(s) = (DoG.slice(s) == DoG_max.slice(s));
}

让我感到困惑的是按位AND 运算符,这不是犰狳提供的。我想知道我的代码的逻辑是否与is_valid = (DoG == DoG_max) & (DoG >= threshold);一致?根据我的调查,结果与matlab中的结果不同。

如果有使用Eigen的解决方案,也请告诉我!

解决方法

&&运算符是在Armadillo中实现的,但奇怪的是它没有记录。尝试将其翻译为Matlab代码:

ucube is_valid = (DoG == DoG_max) && (DoG >= threshold);

如果需要标量输出,请尝试以下操作:

bool is_valid = all(vectorise((DoG == DoG_max) && (DoG >= threshold)));

C ++和Matlab之间的“&”和“ &&”含义有些混淆。在C ++中,“&”表示“按位与”,而“ &&”表示“逻辑与”。 https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

在Matlab中,“&”和“ &&”均表示“逻辑与”,但根据上下文,其评估略有不同:What's the difference between & and && in MATLAB?

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...