I want to write the following matlab code in Eigen
(where K
is pxp
and W
is pxb
):
H = (K*W)>0;
However the only thing that I came up so far is:
H = ((K*W.array() > 0).select(1,0));
This code doesn't work as explained here, but replacing 0
with VectorXd::Constant(p,0)
(as suggested in the link question) generates a runtime error:
Eigen::internal::variable_if_dynamic<T, Value>::variable_if_dynamic(T) [with T = long int; int Value = 1]: Assertion `v == T(Value)' failed.
How can I solve this?
You don't need
.select()
. You just need to cast an array ofbool
to an array ofH
's component type.Your original attempt failed because the size of your constant 1/0 array is not match with the size of
H
. UsingVectorXd::Constant
is not a good choice whenH
isMatrixXd
. You also have a problem with parentheses. I think you want*
rather than.*
in matlab notation.When calling a template member function in a template, you need to use the
template
keyword.See this for some explanation.
Issue casting C++ Eigen::Matrix types via templates