复习!复习!

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

1.代码:

Matlab相关系数的意义:

  1. Eigen::MatrixXf correlation_matrix = corrcoef( LocM );

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

翻译成Eigen:

还是自己写个函数吧

//1.求协方差

  1. Eigen::MatrixXf CIcSearchM::cov(Eigen::MatrixXf &d1, Eigen::MatrixXf &d2)
  2. {
  3.     Eigen::MatrixXf  CovM(1,1);
  4.     assert(1 ==d1.cols() && 1 ==d2.cols() &&d1.cols()==d2.cols()  );
  5.  
  6.     //求协方差
  7.     float Ex =0;float Ey=0;
  8.     for (int i=0;i< d1.rows();++i){
  9.         Ex +=d1(i);
  10.         Ey +=d2(i);
  11.     }
  12.     Ex /=d1.rows();
  13.     Ey /=d2.rows();
  14.  
  15.     for (int i=0;i< d1.rows();++i){
  16.         CovM(0) += (d1(i)-Ex)*(d2(i)-Ey);
  17.     }
  18.     CovM(0) /= d1.rows() -1;
  19.     return CovM;
  20. }

//2.写入方差矩阵

  1. //求矩阵的相关系数!
  2. //返回矩阵A的列向量的相关系数矩阵//对行向量求相关系数 , 与行数无关,返回 cols()*cols() 矩阵...
  3. Eigen::MatrixXf CIcSearchM::corrcoef(Eigen::MatrixXf &M)
  4. {
  5. // C(i,j)/SQRT(C(i,i)*C(j,j)).//C is the covariation Matrix
  6. int Row= M.rows();
  7. int Col= M.cols();
  8. int Order= Col;//int Order= (std::max)(Row,Col);
  9.  
  10. Eigen::MatrixXf Coef(Order,Order);
  11. for (int i=0;i<Order;++i){
  12. for (int j=0;j<Order;++j){
  13. Coef(i,j)= cov((Eigen::MatrixXf)M.col(i),(Eigen::MatrixXf)M.col(j))(0);
  14. }
  15. }
  16. return Coef;
  17. }


2.优化的代码

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

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

优化的代码:

  1. //求矩阵的相关系数!一个原始公式的简化算法/优化算法
  2. //返回矩阵A的列向量的相关系数矩阵//对行向量求相关系数 , 与行数无关,返回 cols()*cols() 矩阵...
  3. Eigen::MatrixXf CIcSearchM::CorrcoefOpm(Eigen::MatrixXf &MI)
  4. {
  5. Eigen::MatrixXf M =MI;
  6. // C(i,j)/SQRT(C(i,i)*C(j,j)).//C is the covariation Matrix
  7. //公式:
  8. //temp = mysample - repmat(mean(mysample), 10, 1);
  9. //result = temp' * temp ./ (size(mysample, 1) - 1)
  10. int Row= M.rows();
  11. int Col= M.cols();
  12. int Order= Col;//int Order= (std::max)(Row,Col);
  13.  
  14. SYSTEMTIME sysP;
  15. GetLocalTime( &sysP );
  16. int MileTsp = sysP.wSecond;
  17. int MileTP = sysP.wMilliseconds;
  18.  
  19. Eigen::MatrixXf CovM(Order,Order);//(1,Col);
  20. Eigen::MatrixXf E_M(1,Col);
  21. //减去每一个维度的均值;确定一列为一个维度。
  22. //std::cout<< "Mat Src :"<<std::endl;m_Testor.print_EigenMat( M);
  23. for (int i =0;i< Col;++i)
  24. {
  25. //求均值
  26. E_M(i) =M.col(i).sum()/M.rows();
  27. //std::cout<< "E_M(i)" << E_M(i)<< std::endl;
  28. M.col(i) = M.col(i)- E_M(i);
  29. //
  30. }
  31.  
  32. //SYSTEMTIME sysP2;
  33. //GetLocalTime( &sysP );
  34. //int MileTsp2 = sysP.wSecond;
  35. //int MileTP2 = sysP.wMilliseconds;
  36. //int DetaTp = MileTP2 - MileTP;
  37. //int DetaTsp = MileTsp2 -MileTsp;
  38. //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
  39. //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
  40.  
  41. //std::cout<< "Mat E_M :"<<std::endl;m_Testor.print_EigenMat( M);
  42. CovM = M.transpose();
  43. //GetLocalTime( &sysP );
  44. //MileTsp2 = sysP.wSecond;
  45. //MileTP2 = sysP.wMilliseconds;
  46. //DetaTp = MileTP2 - MileTP;
  47. //DetaTsp = MileTsp2 -MileTsp;
  48. //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
  49. //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
  50.  
  51. //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
  52. CovM = CovM * M ;
  53. //GetLocalTime( &sysP );
  54. //MileTsp2 = sysP.wSecond;
  55. //MileTP2 = sysP.wMilliseconds;
  56. //DetaTp = MileTP2 - MileTP;
  57. //DetaTsp = MileTsp2 -MileTsp;
  58. //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
  59. //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
  60.  
  61. //实现 ./ 函数 数值计算没有区别
  62. CovM = CovM /(Order-1)/(Order-1);
  63. //GetLocalTime( &sysP );
  64. //MileTsp2 = sysP.wSecond;
  65. //MileTP2 = sysP.wMilliseconds;
  66. //DetaTp = MileTP2 - MileTP;
  67. //DetaTsp = MileTsp2 -MileTsp;
  68. //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
  69. //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
  70.  
  71. //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
  72. //GetLocalTime( &sysP );
  73. //MileTsp2 = sysP.wSecond;
  74. //MileTP2 = sysP.wMilliseconds;
  75. //DetaTp = MileTP2 - MileTP;
  76. //DetaTsp = MileTsp2 -MileTsp;
  77. //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
  78. //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
  79.  
  80. //遍历一次
  81. for (int i=0;i< Order;++i){
  82. for (int j=0;j<Order;++j){
  83. CovM(i,j) = sqrt(CovM(i,i)*CovM(j,j) );
  84. }
  85. }
  86.  
  87. //GetLocalTime( &sysP );
  88. //MileTsp2 = sysP.wSecond;
  89. //MileTP2 = sysP.wMilliseconds;
  90. //DetaTp = MileTP2 - MileTP;
  91. //DetaTsp = MileTsp2 -MileTsp;
  92. //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
  93. //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
  94.  
  95. //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
  96. return CovM;
  97. }

稀疏矩阵可以加速到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. 学习记录--让我打开另一种思路的SQL

    1.显示文章.提交人和最后回复时间 select a.title,a.username,b.adddate from table a, (select max(adddate) adddate fro ...

  2. Python网络请求urllib和urllib3详解

    Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...

  3. 【POJ 1860】Currency Exchange

    [题目链接]:http://poj.org/problem?id=1860 [题意] 给你n种货币,m种货币之间的交换信息; 交换信息以 A,B,RA,CA,RB,CB的形式给出; 即A换B的话假设A ...

  4. [POJ1226]Substrings(后缀数组)

    传送门 给定 n 个字符串,求出现或反转后出现在每个字符串中的最长子串. 算法分析: 这题不同的地方在于要判断是否在反转后的字符串中出现.其实这并没有加大题目的难度. 只需要先将每个字符串都反过来写一 ...

  5. 清北学堂模拟赛d3t3 c

    分析:一开始拿到这道题真的是无从下手,暴力都很难打出来.但是基本的方向还是要有的,题目问的是方案数,dp不行就考虑数学方法.接下来比较难想.其实对于每一行或者每一列,我们任意打乱顺序其实对答案是没有影 ...

  6. 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?

    这里的疑问在于pageContext和request都是JSP中的内置对象之一,为什么不直接用${request.contextPath}来获取项目路径? 出现这种疑问,其实是将JSP的内置对象和EL ...

  7. R语言 PCA

    1.关键点 综述:主成分分析 因子分析 典型相关分析,三种方法的共同点主要是用来对数据降维处理的从数据中提取某些公共部分,然后对这些公共部分进行分析和处理. #主成分分析 是将多指标化为少数几个综合指 ...

  8. ESXi License过期解决办法

    http://blog.sina.com.cn/s/blog_538439270101pqls.html

  9. asp.net--mvc--异步编程

    Using Asynchronous Methods in ASP.NET MVC 4 asp.net mvc中的异步只能增加系统的性能,原来需要500个线程的,现在需要50个就够了,对一些常规的程序 ...

  10. Unity图片变灰的方式

    http://www.tuicool.com/articles/Vruuqme NGUI中的Button差点儿是最经常使用到的控件之中的一个,而且能够组合各种组件(比方UIButtonColor,UI ...