复习!复习!

原文链接:http://blog.csdn.net/goodshot/article/details/8611178

1.代码:

Matlab相关系数的意义:

Eigen::MatrixXf  correlation_matrix = corrcoef( LocM );

对行向量求相关系数 , 与列数无关,返回 cols()*cols() 矩阵...

翻译成Eigen:

还是自己写个函数吧

//1.求协方差

Eigen::MatrixXf CIcSearchM::cov(Eigen::MatrixXf &d1, Eigen::MatrixXf &d2)
{
    Eigen::MatrixXf  CovM(1,1);
    assert(1 ==d1.cols() && 1 ==d2.cols() &&d1.cols()==d2.cols()  );     //求协方差
    float Ex =0;float Ey=0;
    for (int i=0;i< d1.rows();++i){
        Ex +=d1(i);
        Ey +=d2(i);
    }
    Ex /=d1.rows();
    Ey /=d2.rows();     for (int i=0;i< d1.rows();++i){
        CovM(0) += (d1(i)-Ex)*(d2(i)-Ey);
    }
    CovM(0) /= d1.rows() -1;
    return CovM;
}

//2.写入方差矩阵

//求矩阵的相关系数!
//返回矩阵A的列向量的相关系数矩阵//对行向量求相关系数 , 与行数无关,返回 cols()*cols() 矩阵...
Eigen::MatrixXf CIcSearchM::corrcoef(Eigen::MatrixXf &M)
{
// C(i,j)/SQRT(C(i,i)*C(j,j)).//C is the covariation Matrix
int Row= M.rows();
int Col= M.cols();
int Order= Col;//int Order= (std::max)(Row,Col); Eigen::MatrixXf Coef(Order,Order);
for (int i=0;i<Order;++i){
for (int j=0;j<Order;++j){
Coef(i,j)= cov((Eigen::MatrixXf)M.col(i),(Eigen::MatrixXf)M.col(j))(0);
}
}
return Coef;
}


2.优化的代码

使用Eigen计算1000维的方阵大概需要200ms的时间,相对于matlab默认开启GPU加速,时间上消耗的太多了。

参考:比较OpenBLAS、Matlab、MKL、Eigen的基础计算性能

优化的代码:

//求矩阵的相关系数!一个原始公式的简化算法/优化算法
//返回矩阵A的列向量的相关系数矩阵//对行向量求相关系数 , 与行数无关,返回 cols()*cols() 矩阵...
Eigen::MatrixXf CIcSearchM::CorrcoefOpm(Eigen::MatrixXf &MI)
{
Eigen::MatrixXf M =MI;
// C(i,j)/SQRT(C(i,i)*C(j,j)).//C is the covariation Matrix
//公式:
//temp = mysample - repmat(mean(mysample), 10, 1);
//result = temp' * temp ./ (size(mysample, 1) - 1)
int Row= M.rows();
int Col= M.cols();
int Order= Col;//int Order= (std::max)(Row,Col); SYSTEMTIME sysP;
GetLocalTime( &sysP );
int MileTsp = sysP.wSecond;
int MileTP = sysP.wMilliseconds; Eigen::MatrixXf CovM(Order,Order);//(1,Col);
Eigen::MatrixXf E_M(1,Col);
//减去每一个维度的均值;确定一列为一个维度。
//std::cout<< "Mat Src :"<<std::endl;m_Testor.print_EigenMat( M);
for (int i =0;i< Col;++i)
{
//求均值
E_M(i) =M.col(i).sum()/M.rows();
//std::cout<< "E_M(i)" << E_M(i)<< std::endl;
M.col(i) = M.col(i)- E_M(i);
//
} //SYSTEMTIME sysP2;
//GetLocalTime( &sysP );
//int MileTsp2 = sysP.wSecond;
//int MileTP2 = sysP.wMilliseconds;
//int DetaTp = MileTP2 - MileTP;
//int DetaTsp = MileTsp2 -MileTsp;
//std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
//std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl; //std::cout<< "Mat E_M :"<<std::endl;m_Testor.print_EigenMat( M);
CovM = M.transpose();
//GetLocalTime( &sysP );
//MileTsp2 = sysP.wSecond;
//MileTP2 = sysP.wMilliseconds;
//DetaTp = MileTP2 - MileTP;
//DetaTsp = MileTsp2 -MileTsp;
//std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
//std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl; //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
CovM = CovM * M ;
//GetLocalTime( &sysP );
//MileTsp2 = sysP.wSecond;
//MileTP2 = sysP.wMilliseconds;
//DetaTp = MileTP2 - MileTP;
//DetaTsp = MileTsp2 -MileTsp;
//std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
//std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl; //实现 ./ 函数 数值计算没有区别
CovM = CovM /(Order-1)/(Order-1);
//GetLocalTime( &sysP );
//MileTsp2 = sysP.wSecond;
//MileTP2 = sysP.wMilliseconds;
//DetaTp = MileTP2 - MileTP;
//DetaTsp = MileTsp2 -MileTsp;
//std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
//std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl; //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
//GetLocalTime( &sysP );
//MileTsp2 = sysP.wSecond;
//MileTP2 = sysP.wMilliseconds;
//DetaTp = MileTP2 - MileTP;
//DetaTsp = MileTsp2 -MileTsp;
//std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
//std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl; //遍历一次
for (int i=0;i< Order;++i){
for (int j=0;j<Order;++j){
CovM(i,j) = sqrt(CovM(i,i)*CovM(j,j) );
}
} //GetLocalTime( &sysP );
//MileTsp2 = sysP.wSecond;
//MileTP2 = sysP.wMilliseconds;
//DetaTp = MileTP2 - MileTP;
//DetaTsp = MileTsp2 -MileTsp;
//std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
//std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl; //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
return CovM;
}

稀疏矩阵可以加速到3ms,我去!终于可以实用了.....

EKF优化:协方差coff计算公式、意义、Code优化的更多相关文章

  1. Linux启动时间优化-内核和用户空间启动优化实践

    关键词:initcall.bootgraph.py.bootchartd.pybootchart等. 启动时间的优化,分为两大部分,分别是内核部分和用户空间两大部分. 从内核timestamp 0.0 ...

  2. 基于Oracle的SQL优化(社区万众期待 数据库优化扛鼎巨著)

    基于Oracle的SQL优化(社区万众期待数据库优化扛鼎巨著) 崔华 编   ISBN 978-7-121-21758-6 2014年1月出版 定价:128.00元 856页 16开 编辑推荐 本土O ...

  3. mysql数据库优化课程---18、mysql服务器优化

    mysql数据库优化课程---18.mysql服务器优化 一.总结 一句话总结: 1.四种字符集问题:字符集都设置为utf-82.slow log慢查询日志问题3.root密码丢失 1.mysql存在 ...

  4. ORACLE性能优化- Buffer cache 的调整与优化

    Buffer Cache是SGA的重要组成部分,主要用于缓存数据块,其大小也直接影响系统的性能.当Buffer Cache过小的时候,将会造成更多的 free buffer waits事件. 下面将具 ...

  5. 转 cocos2d-x 优化(纹理渲染优化、资源缓存、内存优化)

    概述 包括以下5种优化:引擎底层优化.纹理优化.渲染优化.资源缓存.内存优化   引擎优化 2.0版本比1.0版本在算法上有所优化,效率更高.2.0版本使用OpenGl ES 2.0图形库,1.0版本 ...

  6. Android开发优化之——对Bitmap的内存优化

    http://blog.csdn.net/arui319/article/details/7953690 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitm ...

  7. MySql数据库3【优化2】sql语句的优化

    1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...

  8. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十二)数据层优化-explain关键字及慢sql优化

    本文提要 从编码角度来优化数据层的话,我首先会去查一下项目中运行的sql语句,定位到瓶颈是否出现在这里,首先去优化sql语句,而慢sql就是其中的主要优化对象,对于慢sql,顾名思义就是花费较多执行时 ...

  9. 数据库性能优化(database tuning)性能优化绝不仅仅只是索引

    一毕业就接触优化方面的问题,专业做优化也有至少5年之多的时间了,可现在还是经常听到很多人认为优化很简单,就是建索引的问题,这确实不能怪大家,做这行20多年的时间里,在职业生涯的每个阶段,几乎都能听到这 ...

随机推荐

  1. 使用命令行打开vscode

    今天看到一个博客,直接使用code . 就可以打开vscode

  2. BUPT2017 wintertraining(16) #9

    龟速补题.目前基本弃坑.已暂时放弃 D.I 两题. 下面不再写题意了直接说解法注意事项之类,直接放contest链接. https://vjudge.net/contest/151537 A.The ...

  3. (41)Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

    已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring Boot中使用多数据源做准备. 从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这 ...

  4. 洛谷 P3258 BZOJ 3631 [JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  5. 一次源码编译PHP折腾记

    前言LINUX环境下编译安装是很折腾人的一件事情,如果没有C/C++功底,碰到编译器报错,肯定要抓狂了 :):),有些软件需要依赖其它库,必须先把依赖库安装好才能进行软件安装.当你学会了编译安装神技之 ...

  6. java 基础编程day1

    public class TextCharType{ public static void(Sting[] args){ char c1 = 'a'; char c2 = '了'; System.ou ...

  7. POJ 3653 &amp; ZOJ 2935 &amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

    题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http ...

  8. Zepto Code Rush 2014-A. Feed with Candy(HACK)

    A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. 美团网 KVM虚拟化公开课学习笔记

    KVM优化技术,美团开放平台--邱剑 基于KVM现有选项做一些优化.视频地址:http://www.osforce.cn/course/77/learn#lesson/80 CPU调优: 1.Cont ...

  10. CSS学习(十四)-CSS颜色之中的一个

    一.理论: 1.RGB色彩模式  a.CMYK色彩模式  b.索引色彩模式 (主要用于web) c.灰度模式  d.双色调模式 2.opacity: a.alphavalue:透明度 b.inheri ...