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

  • Home
  • SEARCH
  • 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 7666625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:52:49+00:00 2026-05-31T14:52:49+00:00

I’m looking for good study material about computation of weighted median algorithm and/or sample

  • 0

I’m looking for good study material about computation of weighted median algorithm and/or sample code in C++. The weights of my median are values between 0 and 1. Could you recommend me some links?

  • 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-05-31T14:52:50+00:00Added an answer on May 31, 2026 at 2:52 pm

    The weighted median is defined like so:

    If x is a sorted array of N elements, and w is the array of weights with a total weight W, then the weighted median is the last x[i] such that the sum of w[i] and of all previous weights are less than or equal to S/2.

    In C++, this can be expressed like so (assuming x, w and W are defined as above)

    double sum = 0;
    int i;
    for(i = 0; i < N; ++i)
    {
        sum += w[i];
        if(sum > W/2)
            break;
    }
    double median = x[i-1];
    

    EDIT

    So it seems I answered this question too hastily and made some mistakes. I found a neat description of weighted median from the R documentation, which describes it like so:

    For the n elements x = c(x[1], x[2], ..., x[n]) with positive
    weights w = c(w[1], w[2], ..., w[n]) such that sum(w) = S, the
    weighted median is defined as the element x[k] for which initial the
    total weight of all elements x[i] < x[k] is less or equal to S/2
    and for which the total weight of all elements x[i] > x[k] is less
    or equal to S/2.

    From this description, we have a pretty straight-forward implementation of the algorithm. If we start with k == 0, then there are no elements before x[k], so the total weight of elements x[i] < x[k] will be less than S/2. Depending on the data, the total weight of the elements x[i] > x[k] may or may not be less than S/2. So we can just move forward through the array until this second sum becomes less than or equal to S/2:

    #include <cstddef>
    #include <numeric>
    #include <iostream>
    
    int main()
    {
      std::size_t const N = 5;
      double x[N] = {0, 1, 2, 3, 4};
      double w[N] = {.1, .2, .3, .4, .5};
    
      double S = std::accumulate(w, w+N, 0.0); // the total weight
    
      int k = 0;
      double sum = S - w[0]; // sum is the total weight of all `x[i] > x[k]`
    
      while(sum > S/2)
      {
        ++k;
        sum -= w[k];
      }
    
      std::cout << x[k] << std::endl;
    }
    

    Note that if the median is the last element (medianIndex == N-1), then sum == 0, so the condition sum > S/2 fails. Thus, k will never be out of bounds (unless N == 0!). Also, if there are two elements that satisfy the condition, the algorithm always selects the first one.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I would like to count the length of a string with PHP. The string

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.