- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想得到一个未排序的变量的加权中位数长度, Eigen c++ vectorXf 对象。看来我可以使用 boost来自 boost 统计累加器的 weighted_median 函数库来有效地做到这一点 [?]。
本质上,我正在尝试做一些与已完成的非常相似的事情 here .我不确定 boost 的累加器是正确的框架对于这个任务(如果不请建议!),但我还没有找到另一个O(n) 加权中位数的现成实现。
此时我的问题是是否有办法替换“for(int i=0;i<100;i++)”下面的循环更优雅?
附言我看过this所以问题,但事实并非如此真的很清楚如何将那里的答案变成可操作的解决方案。
#include <Eigen/Dense>
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/median.hpp>
#include <boost/accumulators/statistics/weighted_median.hpp>
using namespace boost::accumulators;
using namespace Eigen;
int main(){
accumulator_set<float, stats<tag::median > > acc1;
accumulator_set<float, stats<tag::median >,int> acc2;
VectorXi rw=VectorXi::Random(100);
VectorXf rn=VectorXf::Random(100);
rw=rw.cwiseAbs();
for(int i=0;i<100;i++){
acc1(rn(i));
acc2(rn(i),weight=rw(i));
}
std::cout << " Median: " << median(acc1) << std::endl;
std::cout << "Weighted Median: " << median(acc2) << std::endl;
return 0;
}
最佳答案
您要做的是使用 boost 累加器在某种容器中累加值。您会注意到,即使通过 std::vector<float>
到蓄能器是行不通的。蓄能器根本不应该以这种方式使用。当然,您可以使用累加器来累加 vector 值或矩阵值 - 但这不是您想要的。
您可以使用 std::for_each
摆脱显式循环,仅此而已:
// median
using boost::bind;
using boost::ref;
std::for_each(rn.data(), rn.data()+rn.rows(), bind<void>( ref(acc1), _1 ) );
question你链接到的在 Eigen3 的最新版本中不再相关。那里给出的代码运行良好并产生正确的结果。
关于c++ - boost++::weighted_median 和 eigen::vectorXf --newbie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11147761/
以下是我的学校项目的完整问题陈述(请耐心等待冗长的内容)。请告诉我如何进行以及开始的先决条件是什么。我想在Linux下用C语言做这个项目。我对 C 语言很熟悉,但没有套接字编程/客户端-服务器编程经验
你好,我才刚刚开始使用基本的 JavaScript。 Write a program which will accept a year from the user and display whethe
嘿,所以我最近开始学习C++,我不知道为什么这个问题总是会给人带来错误的肯定。 也许有人可以帮忙? // [Included stuff] using namespace std; int main(
我决定学习一些Java。因此我一直在网上搜索一些练习。现在我陷入困境,非常感谢您的帮助。 我想计算我从地面扔出的球的最大高度。因此,我使用高度 = 0. 5 * 速度 * 速度(以米每秒为单位)/9.
刚开始使用 C#(使用 VS2008)- 我是一名经验丰富的 Java、C++、VC++、VB 程序员。 我从 codescales.com 下载了一个程序集(我猜);这是一个 dll。 在我的(新)
我是 SQL 新手,我正在尝试完成以下任务:我需要创建一个查询,该查询将根据一组日期返回一组结果。这是我的表格/列: 订单行 order_id, line_no, qty_shipped, total
嘿,我已经成功地隐藏了一些 javascript,当单击 div 时,它将打开一个面板并关闭其他打开的面板。该代码可以工作,但我知道有一种更简洁的方法可以做到这一点,就好像我的菜单有 100 个元素一
这是我第一次需要使用泛型和引用,我遇到了困难。我知道这很明显。 public class Program { void SWAP(ref T a, ref T b) { T dum = a;
所以我只是在学习用 super 基本的 CSS 制作一个网页,看起来应该是这样的: i put the class name in red 但是当我输入代码时,是这样的: 2do parcia
我创建了我的网站的移动版本,它是一个单页网络应用程序。我在前端使用 php 服务器端和 js+jquery。 如何将移动浏览器自动重定向到移动网络应用程序搜索透明的方式引擎? 我应该只允许移动网站上的
我是 Android 的新手,第一次尝试 Fragments。我使用了一个正在 fragment 中转换的 ListView Activity 。但不知何故,由 findViewById() 检索到的
我在尝试实现这个时遇到了问题(所有 goroutine 都在 sleep - 死锁!)这是代码的要点: var workers = runtime.NumCPU() func main() {
我一直在阅读有关 Ada 的文章,它看起来是一种很棒且成熟的语言。但是尽管它的历史和现实世界的使用(甚至是一个非常好的 IDE!),它并不是很“互联网流行”。所以我现在正在寻找资源,因为 Google
我有一个包含多列的 excel 表,其中三个名为 X Y Z。我正在尝试使用 panda 数据框将相同的公式应用于三列。 我需要从该列中的每个单元格值中减去该列中的最后一个值;保存新值并继续往下走。有
我想乘以 integer与 real数字。这是我的代码: interface uses System.SysUtils, System.Types, System.UITypes, System.
我的代码如下: lat <- 24.419 lon <- 54.502 days <- 1 Tmax <- 44.3 Tmin <- 26.0 tal <- 0.72 BCb_guess <- 0.1
我是 asp.net 的新手,我正在尝试学习 Linq to SQL。所以我找到了两种从数据库中提取的不同方法。普通的linq to SQL方式和直接SQL语句方式。 两种方式我都有,但我想知道哪种方
我是 Websphere MQ 的新手,需要一些新手帮助。我正在使用 Websphere MQ Explorer 连接到队列,但不知道如何编写连接字符串。我有所有详细信息(主机、端口、 channel
我有一个使用 AngularJS 的工作页面,控制台中没有任何错误。现在奇怪的是,还有其他我没有使用 AngularJS 的页面在开发人员的控制台中出现以下错误: Uncaught Error: [$
我在 cart.js 中有函数 getCartItems,我想在另一个类 checkout.js 中调用该函数,我该怎么做? 最佳答案 在包含 checkout.js 之前,您需要在页面中包含脚本文件
我是一名优秀的程序员,十分优秀!