[英]How to count the number of nonzero in the eigen::Tensor<T,2>
If I use the sum of bool tensor, the reuslt is 1 forever 如果我使用布尔张量的总和,则重用永远是1
Eigen::Tensor<int,2> my_rankx (3,3);
my_rankx.setValues(
{
{1, 2, 3},
{0, 0, 0},
{11, 12, 0}
});
auto number_matrix = (my_rankx.constant(static_cast<int>(0))==my_rankx);
cout<<number_matrix.sum()<<endl; // result of cout is 1
(my_rankx.constant(0) == my_rankx)
is a tensor of bool
s, and adding bool
s together results in either false
or true
, which are converted to 0
and 1
respectively. (my_rankx.constant(0) == my_rankx)
是bool
的张量,将bool
加在一起会导致false
或true
分别被转换为0
和1
。 (Incidentally, the static_cast<int>(0)
in the question is redundant because 0
is of type int
.) (顺便说一句,问题中的static_cast<int>(0)
是多余的,因为0
的类型为int
。)
You can use std::count
along with data()
instead: 您可以将std::count
与data()
使用:
std::cout << std::count(my_rankx.data(), my_rankx.data() + my_rankx.size(), 0)
<< "\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.