天天看点

MRPT学习笔记----Matrices and Vectors头文件:MatricesVectors基本矩阵(向量)运算一些优化的矩阵(向量)运算提取子阵矩阵合并

<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&lt;f loat&gt; CMatrixFloat ;

typedef CMatrixTemplateNumeric&lt;double&gt; 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 &lt;&lt; M( 0 , 0 ) &lt;&lt; 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 &lt;&lt; ” I n i t i a l i z e d matr ix : ” &lt;&lt; endl &lt;&lt; N &lt;&lt; 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 &lt;− ’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&lt;f loat&gt; v e c t o r f l o a t ;

typedef s td : : vector&lt;double&gt; 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 &lt;&lt; V &lt;&lt; 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 ” ) &lt;&lt; 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,如需转载请自行联系原作者

继续阅读