Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8155209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:36:16+00:00 2026-06-06T16:36:16+00:00

I would like to get the weighted_median of an unsorted, variable length, eigen c++

  • 0

I would like to get the weighted_median of an unsorted, variable
length, eigen c++ vectorXf object. It seems i can use the boost
weighted_median function from boost’s statistical accumulators
library to do that efficiently [?].

In essence, i’m trying to do something very similar to what is done
here. I’m not sure boost’s accumulator are the right framework
for this task (if not please advice!), but i’ve not found another
out the shelf implementation of the O(n) weighted median out there.

My question at this point is whether there a way to replace the
“for(int i=0;i<100;i++)” loop below by a more elegant construct?

P.S. i’ve seen this SO question, but it’s not
really clear how to turn the answer there unto an
operation-able solution.

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:36:17+00:00Added an answer on June 6, 2026 at 4:36 pm

    What you’re trying to do is to use the boost accumulators to accumulate values in a container of some sort. You’ll notice that even passing std::vector<float> to an accumulator won’t work. Accumulators are simply not meant to be used that way. You can use accumulators to accumulate vector- or matrix-valued values, of course – but that’s not what you’re after here.

    You can use std::for_each to get rid of the explicit loop, and that’s about it:

    // median
    using boost::bind;
    using boost::ref;
    std::for_each(rn.data(), rn.data()+rn.rows(), bind<void>( ref(acc1), _1 ) );
    

    The question you link to is not relevant anymore in the latest release version of Eigen3. The code given there runs just fine and produces correct results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like get all data into an object with request ajax. This data
I would like to get numbers of comments for each posts using mysql. I
I would like to get to know how to redirect output of some program
I would like to get View instance that is used to display specific Preference
I would like to get the generic information ( Counter class) of my CounterPersistence
I would like to get rid of the close/X button on the top right
I would like to get a help from you all. The below is the
I would like to get started developing games with Flixel, but im on a
I would like to get the dimensions (coordinates) for all the HTML elements of
I would like to get the most out of working in cch or tcsh

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.