normalize_rows.h 789 B

123456789101112131415161718192021222324252627282930313233343536
  1. #ifndef IGL_NORMALIZE_ROWS_H
  2. #define IGL_NORMALIZE_ROWS_H
  3. #include <Eigen/Core>
  4. namespace igl
  5. {
  6. // Normalize the rows in A so that their lengths are each 1 and place the new
  7. // entries in B
  8. // Inputs:
  9. // A #rows by k input matrix
  10. // Outputs:
  11. // B #rows by k input matrix, can be the same as A
  12. void normalize_rows(const Eigen::MatrixXd & A, Eigen::MatrixXd & B);
  13. }
  14. void igl::normalize_rows(const Eigen::MatrixXd & A, Eigen::MatrixXd & B)
  15. {
  16. // loop over rows
  17. for(int i = 0; i < A.rows();i++)
  18. {
  19. double length = 0;
  20. // loop over cols
  21. for(int j = 0; j < A.cols();j++)
  22. {
  23. length += A(i,j)*A(i,j);
  24. }
  25. length = sqrt(length);
  26. // loop over cols
  27. for(int j = 0; j < A.cols();j++)
  28. {
  29. B(i,j) = A(i,j) / length;
  30. }
  31. }
  32. }
  33. #endif