normalize_rows.h 854 B

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