复习!复习!

原文链接: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. Django——6 模型基础ORM 数据库连接配置 模型的创建与映射 数据的增删改查

    Django Django的ORM简介 数据库连接配置 模型的创建与映射 数据库的增删改查 增数据 查数据及补充 改数据 删数据   Django的ORM系统分析 ORM概念:对象关系映射(Objec ...

  2. FOJ2250 不可能弹幕结界

    Problem 2250 不可能弹幕结界 Time Limit: 1000 mSec    Memory Limit : 65536 KB Problem Description 咲夜需要穿过一片弹幕 ...

  3. 在Hibernate中使用Memcached作为一个二级分布式缓存

    转自:http://www.blogjava.net/xmatthew/archive/2008/08/20/223293.html   hibernate-memcached--在Hibernate ...

  4. CF #321 (Div. 2) E

    用线段树维护哈希,类似于进位制的一个哈希 a[i]*p^i+a[i-1]*p^i-1... 然后,线段树存在某区间的哈希的值,对于更新,则只需提前计算出整段的哈希值即可. 判断是否相等,由于相隔为d, ...

  5. Hilbert曲线简单介绍及生成算法

    Hilbert曲线 Hilbert曲线是一种填充曲线,相似的填充曲线还包含Z曲线.格雷码等其它方法.Hilbert曲线根据自身空间填充曲线的特性,能够线性地贯穿二维或者更高维度每一个离散单元.而且只穿 ...

  6. AWS OpsWorks新增Amazon RDS支持

    AWS OpsWorks是一个应用管理服务. 你可以通过它把你的应用在一个 堆栈中定义成为不同层的集合.每一个堆栈提供了须要安装和配置的软件包信息,同一时候也能部署不论什么在OpsWorks层中定义的 ...

  7. luogu1081 开车旅行 树上倍增

    题目大意 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市i 的海拔高度为Hi,城市i 和城市j 之间的距离 ...

  8. Sqlserver 数据库恢复常见错误及解决(网站转载 留着备用)

    数据库恢复常见错误及解决 2009-04-13 11:25 1145人阅读 评论(0) 收藏 举报 数据库databasesqlserverusermicrosoftsql server 在sqlSe ...

  9. elasticsearch 索引搜索和索引性能优化配置——思路:去掉不必要的数据,减小数据的磁盘空间占用,同时提升性能

    压缩配置: index.codec: best_compression 合并索引: curl –XPOST localhost:9200/hec_test3/_forcemerge’ 配置mappin ...

  10. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...