直接上代码


#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;

#define ROWS 4 
template <typename Derived>
void vectorDistance(const EigenBase<Derived>& a, const EigenBase<Derived>& b)
{
    const ArrayXf A = a;
    const ArrayXf B = b;
    cout << A << B << endl; 
}


float arrayXfDistance(const Ref<const ArrayXf> x, const Ref<const ArrayXf> y)
{
    return (float)sqrt((x-y).square().sum());
}

int main()
{
    int ids = 2; // person folder number
    MatrixXf globalF(ROWS, ids); // global matrix
    ArrayXf v(3);
    ArrayXf w(3);
    v << 0,
         5,
         1;
    w << 3,
         1.4,
         1;
    cout << arrayXfDistance(w,v) << endl;
    cout << w.matrix() << endl;
    vectorDistance(w,v);

    MatrixXf m = MatrixXf::Random(ROWS, 2);
    cout << m << endl;
    globalF = m;
    MatrixXf k = MatrixXf::Random(ROWS,1);
    cout << k << endl;
    for(int t=1; t<10; t++)
    {
        MatrixXf tmp(ROWS,ids+t-1);
        tmp = globalF;
        globalF.resize(ROWS, ids+t);  // change global matrix size
        cout << globalF.rows() << " "  << globalF.cols() << endl;   
        globalF << tmp,k;  // append new feature in the end
        cout << globalF << endl;
        cout << arrayXfDistance(globalF.col(1).array(),globalF.col(2).array()) << endl;
    }

}
                                                                             

编译命令:

g++ -I /home/user/workstation/eigen/eigen/ test3.cpp -o test3