egen3 rowwise().maxCoeff()的使用

34 篇文章 26 订阅

1、安装eigen3 

2、引用头文件

3、代码测试


    MatrixXf aaa(2, 4);
    aaa << 1, 2, 3, 4,  
        5, 6, 7, 8;  
    Vector2f  diff(10, 20);
    aaa.colwise() += diff;

    std::cout << "new_aaa : " << aaa << endl;   
 

 

全部代码:


int main()
{
    MatrixXf mat(2, 4);
    mat << -1, 2, 6, 19,   //19+6+2-1= 26
        3, 1, 7, -2;   // 3+1+7-2=9
    VectorXf maxVal = mat.rowwise().maxCoeff();  // 计算矩阵钟每行的最大值  返回的是一个行最大值组成的一个向量
    VectorXf minVal = mat.rowwise().minCoeff();    // 同上
     

    std::cout << "Maxima at positions " << endl;
   // std::cout << maxIndex << std::endl;
    std::cout << "maxVal  : " << maxVal << endl;
    std::cout << "minVal : " << minVal << endl;

    VectorXf  difference = maxVal - minVal;   //  对应的行  maxVal[i]-minVal[i],返回的也是一个向量
    std::cout << "difference : " << difference << endl;
    Vector2f mm(-1,-2);
    std::cout << "difference.norm() : " << difference.norm() << endl;  // 计算模 
    std::cout << "mm.norm() : " << mm.norm() << endl;

    //  计算每行的和 返回的是一个行向量 
    Vector2f source_mea=mat.rowwise().sum();
    std::cout << "source_mea.norm() : " << source_mea<< endl;


    cout << "矩阵的列数     " << mat.cols() << endl;  // 4 

    //  计算没行的平均值
    Vector2f  mean=source_mea / 4;
    std::cout << "mean    : " << mean << endl;



    MatrixXf vertices_source(2, 4);
    vertices_source << -1, 2, 6, 19,   //19+6+2-1= 26
        3, 1, 7, -2;   // 3+1+7-2=9

    MatrixXf vertices_target(2, 4);

    vertices_target << 0, 2, 6, 19,   //19+6+2-1= 26
        3, 1, 7, 2;   // 3+1+7-2=9

    //最大 最小 
   Vector2f source_scale = vertices_source.rowwise().maxCoeff() - vertices_source.rowwise().minCoeff();
   Vector2f target_scale = vertices_target.rowwise().maxCoeff() - vertices_target.rowwise().minCoeff();


    double scale = std::max(source_scale.norm(), target_scale.norm());
    std::cout << "scale =      " << scale << std::endl;
    vertices_source /= scale;
    vertices_target /= scale;

    std::cout << "vertices_source : " << vertices_source << endl;
    std::cout << "vertices_target : " << vertices_target << endl;
    /// De-mean
    Vector2f source_mean, target_mean;

    source_mean = vertices_source.rowwise().sum() / double(vertices_source.cols());
    target_mean = vertices_target.rowwise().sum() / double(vertices_target.cols());

    std::cout << "source_mean : " << source_mean << endl;
    std::cout << "target_mean : " << target_mean << endl;

    vertices_source.colwise() += source_mean;
    vertices_target.colwise() += target_mean;

    std::cout << "vertices_source : " << vertices_source << endl;
    std::cout << "vertices_target : " << vertices_target << endl;



    std::cout << "aaa =========================================== : " << endl;

    MatrixXf aaa(2, 4);
    aaa << 1, 2, 3, 4,   //
        5, 6, 7, 8;   // 
    Vector2f  diff(10, 20);
    aaa.colwise() += diff;

    std::cout << "new_aaa : " << aaa << endl;

    system("pause");
    return 0;

}

example:


#if  1
int main()
{

    typedef double Scalar;
    typedef Eigen::Matrix<Scalar, 3, Eigen::Dynamic> Vertices;
    typedef Eigen::Matrix<Scalar, 3, 1> VectorN;
  //--- Model that will be rigidly transformed
    Vertices vertices_source(3,4);
    vertices_source << 1, 3, 5, 6,
        2, 4, 12, 15,
        4,16,17,56;
    // 三行(xyz)  点数列的矩阵
    std::cout << "vertices_source:     " << vertices_source.rows() << "        x        " << vertices_source.cols() << std::endl;

    //--- Model that source will be aligned to
    Vertices vertices_target(3,4);
    vertices_target << 1, 2, 3, 4,
        5,6,7,8,
        4, 12, 14, 16;
    // 三行(xyz)  点数列的矩阵
    std::cout << "vertices_target:     " << vertices_target.rows() << "       x        " << vertices_target.cols() << std::endl;



    // scaling  测量
    Eigen::Vector3d source_scale, target_scale;
    //                                 6   15   56                               // 1  2 4 
    source_scale = vertices_source.rowwise().maxCoeff() - vertices_source.rowwise().minCoeff();  // 5 13 52
    target_scale = vertices_target.rowwise().maxCoeff() - vertices_target.rowwise().minCoeff();  // 3 3 12 
    cout << "source_scale    :" << source_scale << endl;   // 5 13 52
    cout << "target_scale     : " <<  target_scale  << endl;  // 3 3 12 
    //    vertices_source.rowwise().maxCoeff()   表示的是各行的最大值  xyz 中坐标的最大
       //    vertices_source.rowwise().minCoeff()   表示的是各行的最大值  xyz 中坐标的最小



    // 计算模长  的差        
    std::cout << "source_scale.norm()       " << source_scale.norm() << std::endl;  // 
    double scale = std::max(source_scale.norm(), target_scale.norm());
    // 分别计算出x y z 方向上的最大-最小
    std::cout << "scale =      " << scale << std::endl;  // 
    vertices_source /= scale;
    vertices_target /= scale;

    std::cout << "new    vertices_source   " << vertices_source << std::endl;
    std::cout << "new    vertices_target   " << vertices_target << std::endl;
    /// De-mean
    VectorN source_mean, target_mean;

    source_mean = vertices_source.rowwise().sum() / double(vertices_source.cols());
    target_mean = vertices_target.rowwise().sum() / double(vertices_target.cols());

    std::cout << "new    source_mean   " << source_mean << std::endl;
    std::cout << "new    target_mean   " << target_mean << std::endl;


    vertices_source.colwise() -= source_mean;
    vertices_target.colwise() -= target_mean;


    std::cout << "new ----    vertices_source   " << vertices_source << std::endl;
    std::cout << "new ----    vertices_target   " << vertices_target << std::endl;
    system("pause");
    return 0;
}


#endif // 

 

先更新到此,后续碰到再说。。。。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值