Matlab和任何理解上的困难
我无法理解这段代码的作用:
if any(scale==0)
loglik = -inf;
我必须使用犰狳库将其转换为 C++,该库没有 any
函数,所以我想知道如何做到这一点。
我已经阅读了 matlab 手册,但仍然令人困惑。 然后我尝试了这个测试用例:
if any([*]==0)
1
else
0
end
使用 as * 这些值(以及更多):
[0 0;0 0]
[1 0;0 0]
[1 0;1 0]
[1 0;0 1]
[1 1;1 1]
但我仍然很困惑,结果很暗..请问有什么解释吗?
I can't understand what this code does:
if any(scale==0)
loglik = -inf;
I have to translate it into C++ with the Armadillo library, that does not have an any
function so I was wondering how to do it.
I've read the matlab manuals, but it is still confusing.
I've then tried with this test case:
if any([*]==0)
1
else
0
end
using as * those values (and many more):
[0 0;0 0]
[1 0;0 0]
[1 0;1 0]
[1 0;0 1]
[1 1;1 1]
but I am still confused and results are dark.. any explanation please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Matlab(和犰狳)将
0
表示为false
,将布尔值表示为1
(或者任何非0
)真
。它们都在矩阵/向量上定义了一个==
运算符,该运算符执行按组件比较并输出布尔值矩阵。any
正在接受该布尔矩阵并检查是否有非零值。Armadillo 似乎没有
any
,但它确实提供了find
,可用于实现any
:是等效的(尽管可能更慢):它构造一个最多包含一个非零元素的向量(find 文档)。如果它为空,则不存在非零元素,因此
any
将返回 false。Matlab (and Armadillo) represent booleans as
0
forfalse
and1
(or really anything that's not0
) fortrue
. They both define an==
operator over matrices/vectors that does component-wise comparisons and outputs a matrix of booleans.any
is taking in that matrix of booleans and checking if any are non-zero.Armadillo does not appear to have
any
, but it does providefind
which can be used to implementany
:is equivalent (albeit possibly slower): it constructs a vector of at most one non-zero elements (documentation for find). If it's empty, then there are no non-zero elements, so
any
would return false.从版本 3.910 开始,Armadillo 具有 any() 函数。
As of version 3.910, Armadillo has the any() function.