normalize_rows.h 873 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. inline void normalize_rows(const Eigen::MatrixXd & A, Eigen::MatrixXd & B);
  13. }
  14. // Implementation
  15. inline void igl::normalize_rows(const Eigen::MatrixXd & A, Eigen::MatrixXd & B)
  16. {
  17. // Resize output
  18. B.resize(A.rows(),A.cols());
  19. // loop over rows
  20. for(int i = 0; i < A.rows();i++)
  21. {
  22. double length = 0;
  23. // loop over cols
  24. for(int j = 0; j < A.cols();j++)
  25. {
  26. length += A(i,j)*A(i,j);
  27. }
  28. length = sqrt(length);
  29. // loop over cols
  30. for(int j = 0; j < A.cols();j++)
  31. {
  32. B(i,j) = A(i,j) / length;
  33. }
  34. }
  35. }
  36. #endif