问题
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