Suppose I have a MatrixXcf
called A
. I want to replace elements of every column by normalized one relative to the corresponding column. I've written following code but it's not true!
for (int i = 0; i < A.cols(); i++)
A.col(i).real.array() = A.col(i).real().array()/A.col(i).real().norm();
and another question, what's difference between norm()
, normalize()
and normalized()
in Eigen
?
Firstly, you can normalize in place with normalize
, so your code should be:
for (int i = 0; i < A.cols(); i++)
A.col(i).normalize();
Secondly:
normalize
- Normalizes a compile time known vector (as in a vector that is known to be a vector at compile time) in place, returns nothing.normalized
- Returns the above as a constructed copy, doesnt affect the class. You can use it to assign - Vector normCopy = vect.normalized()
.norm
- Returns the norm value of the matrix. Ie, the square root of the sum of the square of all the matrix entries.So the difference is really, what each function returns for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With