You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
896 B

  1. typedef Matrix<float,1,Dynamic> MatrixType;
  2. typedef Map<MatrixType> MapType;
  3. typedef Map<const MatrixType> MapTypeConst; // a read-only map
  4. const int n_dims = 5;
  5. MatrixType m1(n_dims), m2(n_dims);
  6. m1.setRandom();
  7. m2.setRandom();
  8. float *p = &m2(0); // get the address storing the data for m2
  9. MapType m2map(p,m2.size()); // m2map shares data with m2
  10. MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
  11. cout << "m1: " << m1 << endl;
  12. cout << "m2: " << m2 << endl;
  13. cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
  14. cout << "Squared euclidean distance, using map: " <<
  15. (m1-m2map).squaredNorm() << endl;
  16. m2map(3) = 7; // this will change m2, since they share the same array
  17. cout << "Updated m2: " << m2 << endl;
  18. cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
  19. /* m2mapconst(2) = 5; */ // this yields a compile-time error