头文件:
Matrices
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 ;
固定大小矩阵:
文件读写:
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 ’
Vectors
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 ;
基本矩阵(向量)运算
一些优化的矩阵(向量)运算
提取子阵
矩阵合并
本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/11/24/1609761.html,如需转载请自行联系原作者