Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 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 | |
| 6 | MatrixType m1(n_dims), m2(n_dims); |
| 7 | m1.setRandom(); |
| 8 | m2.setRandom(); |
| 9 | float *p = &m2(0); // get the address storing the data for m2 |
| 10 | MapType m2map(p,m2.size()); // m2map shares data with m2 |
| 11 | MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2 |
| 12 | |
| 13 | cout << "m1: " << m1 << endl; |
| 14 | cout << "m2: " << m2 << endl; |
| 15 | cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl; |
| 16 | cout << "Squared euclidean distance, using map: " << |
| 17 | (m1-m2map).squaredNorm() << endl; |
| 18 | m2map(3) = 7; // this will change m2, since they share the same array |
| 19 | cout << "Updated m2: " << m2 << endl; |
| 20 | cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl; |
| 21 | /* m2mapconst(2) = 5; */ // this yields a compile-time error |