Replacing all negative elements with zero eigen3 c++

£可爱£侵袭症+ 提交于 2020-07-08 18:50:34

问题


As said I want to replace all < 0 elements in a eigen3 matrix in C++ with zero in most efficient manner.

I check that there are negative elements using:

(result.array() < 0).any()

回答1:


A nicer and more efficient way than your proposed method would be to use the select method.

result = (result.array() < 0).select(0, result);



回答2:


I found a way: Create a matrix of zeros of same shape,

zero_matrix.setZero();

And find coeff wise maximum between zero matrix and your matrix.

result = result.array().max(zero_matrix.array());


来源:https://stackoverflow.com/questions/40445701/replacing-all-negative-elements-with-zero-eigen3-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!