關于如何用代碼表示矩陣及矩陣的一些算子(三維重建task1-1)
有一段日子沒更新,參加了個數模美賽,弄了一些小專案的資料,過了個年,hh,
接下來的學習主要以代碼解讀為主,參考一下人家大佬寫的代碼,對自己缺少的知識進行補充,邊學邊寫,有說的不對的地方歡迎大家指正,
代碼呈上
#include <iostream>
#include <math/matrix_svd.h>
#include "math/matrix.h"
#include "math/vector.h"
int main(int argc, char *argv[])
{
/*構建一個維度為4x5的矩陣,資料型別為double的矩陣*/
math::Matrix<double, 4, 5> A;
/*矩陣元素的設定和訪問*/
int id=0;
for(int i=0; i< A.rows; i++){
for(int j=0; j< A.cols; j++){
A(i,j) = ++id;
std::cout<<A(i, j)<<" ";
}
std::cout<<std::endl;
}
std::cout<<std::endl;
/*取矩陣的列元素*/
math::Vector<double, 4> col4 = A.col(4); // 取第5列元素
std::cout<<"col4: "<<col4<<std::endl;
/*取矩陣的行元素*/
math::Vector<double, 5> row2 = A.row(2); // 取第3行元素
std::cout<<"row2: "<<row2<<std::endl;
// 向量的創建
math::Vector<double, 5> v1;
for(int i=0; i<v1.dim; i++){
v1[i] = i;
}
std::cout<<"v1: ";
for(int i=0; i<v1.dim; i++){
std::cout<<v1[i]<<" ";
}
std::cout<<std::endl<<std::endl;
//奇異值分解
math::Matrix<double, 4, 5>U;
math::Matrix<double, 5, 5> S, V;
math::matrix_svd<double, 4, 5> (A,&U, &S, &V);
std::cout<<"U: "<<U<<std::endl;
std::cout<<"S: "<<S<<std::endl;
std::cout<<"V: "<<V<<std::endl;
return 0;
}
個人覺得代碼的注釋還是解釋得挺清楚的,不懂的大家可以去查查資料,
Vector和Matrix的簡單記錄
1.矩陣Matrix和向量Vector都是由Matrix類構造的,向量是矩陣的特殊形式,只有一列(列向量)或者一行,
2.Matrix模板類有6個引數,其中前三個引數是必須的,前三個引數如下:
Matrix<typename Scalar,int RowsAtCompileTime,int ColsAtCompileTime >
//Matrix<型別名,矩陣行數, 矩陣列數>
歡迎討論!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/261453.html
標籤:其他
