<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_2.png" target="_blank"></a>
Matrices are implemented as class templates in MRPT, but the followingtwo types are provided for making programs more readable:
typedef CMatrixTemplateNumeric<f loat> CMatrixFloat ;
typedef CMatrixTemplateNumeric<double> CMatrixDouble ;
A matrix with any given size can be created by passing it at construction time, or otherwise it can be resized later as shown in this example:
CMatrixDouble M( 2 , 3 ) ; // Create a 2x3 matrix
cout << M( 0 , 0 ) << endl ; // Pr int out t he l e f t −top element
CMatrixDouble A; // Another way of c r e a t i ng
A. s e t S i z e ( 3 , 4 ) ; // a 2x3 matrix
A( 2 , 3 ) = 1 . 0 ; // Change t he bottom−r i g h t element
A matrix can be resized at any time, and the contents are preserved ifpossible. Notice also in the example how the element at the r’th row and c’th column can be accessed through M(r, c). Sometimes, predefined values must be loaded into a matrix, and writing all the assignments element by element can be tedious and error prone. In those cases, better use this constructor:
const double numbers [ ] = {
1 , 2 , 3 ,
4 ,5 ,6 } ;
CMatrixDouble N(2 , 3 , numbers ) ;
cout << ” I n i t i a l i z e d matr ix : ” << endl << N << endl ;
固定大小矩阵:
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_4.png" target="_blank"></a>
文件读写:
CMatrixDouble H,Z ,D;
H. loadFromTextFi le ( ”H. txt ” ) ; // H <− ’H. t x t ’
H. e i genVe c tor s (Z ,D) ; // Z: e i g env e c t or s , D: e i g e nv a l u e s
Z . saveToTextFi le ( ”Z . txt ” ) ; // Save Z in ’Z. t x t ’
The base class for vectors is the standard STL container std::vector,
typedef s td : : vector<f loat> v e c t o r f l o a t ;
typedef s td : : vector<double> ve c tor doubl e ;
Resize
ve c tor doubl e V( 5 , 1 ) ; // Create a v e c t or wi th 5 ones .
V. r e s i z e ( 1 0 ) ;
cout << V << endl ; // Pr int out t he v e c t or to c onsol e
ve c tor doubl e v ;
loadVector ( CFi leInputStream ( ” in . txt ” ) , v )
ve c tor doubl e v ( 4 , 0 ) ; // [ 0 0 0 0]
vectorToTextFi l e ( v , ”o1 . txt ” ) ; // Save as row
vectorToTextFi l e ( v , ”o2 . txt ” , true ) ; // Append a new row
vectorToTextFi l e ( v , ”o3 . txt ” , fal se , true ) ; // Save as a column
Serializing
If you prefer to serialize the vectors in binary form (see chapter 10), that can be done as simply as:
ve c tor doubl e v = l i n s p a c e ( 0 , 1 , 1 0 0 ) ; // [ 0 . . . 1]
CFi leOutputStream( ”dump. bin ” ) << v ;
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_6.png" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_8.png" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_10.png" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_12.png" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_14.png" target="_blank"></a>
<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/LearnMRPTMatrices_E9A1/image_16.png" target="_blank"></a>
本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/11/24/1609761.html,如需转载请自行联系原作者