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 8462905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:18:31+00:00 2026-06-10T14:18:31+00:00

I am trying to write an efficient ranking algorithm in C++ but I will

  • 0

I am trying to write an efficient ranking algorithm in C++ but I will present my case in R as it is far easier to understand this way.

> samples_x <- c(4, 10, 9, 2, NA, 3, 7, 1, NA, 8)
> samples_y <- c(5, 7, 9, NA, 1, 4, NA, 8, 2, 10)
> orders_x <- order(samples_x)
> orders_y <- order(samples_y)
> cbind(samples_x, orders_x, samples_y, orders_y)
      samples_x orders_x samples_y orders_y
 [1,]         4        8         5        5
 [2,]        10        4         7        9
 [3,]         9        6         9        6
 [4,]         2        1        NA        1
 [5,]        NA        7         1        2
 [6,]         3       10         4        8
 [7,]         7        3        NA        3
 [8,]         1        2         8       10
 [9,]        NA        5         2        4
[10,]         8        9        10        7

Suppose the above is already precomputed. Performing a simple ranking on each of the sample sets takes linear time complexity (the result is much like the rank function):

> ranks_x <- rep(0, length(samples_x))
> for (i in 1:length(samples_x)) ranks_x[orders_x[i]] <- i

For a work project I am working on, it would be useful for me to emulate the following behaviour in linear time complexity:

> cc <- complete.cases(samples_x, samples_y)
> ranks_x <- rank(samples_x[cc])
> ranks_y <- rank(samples_y[cc])

The complete.cases function, when given n sets of the same length, returns the indices for which none of the sets contain NAs. The order function returns the permutation of indices corresponding to the sorted sample set. The rank function returns the ranks of the sample set.

How to do this? Let me know if I have provided sufficient information as to the problem in question.

More specifically, I am trying to build a correlation matrix based on Spearman’s rank sum correlation coefficient test in a way such that NAs are handled properly. The presence of NAs requires that the rankings be calculated for every pairwise sample set (s n^2 log n); I am trying to avoid that by calculating the orders once for every sample set (s n log n) and use a linear complexity for every pairwise comparison. Is this even doable?

Thanks in advance.

  • 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-10T14:18:33+00:00Added an answer on June 10, 2026 at 2:18 pm

    It looks like, when you work out the rank correlation of two arrays, you want to delete from both arrays elements in positions where either has NA.

    You have

    for (i in 1:length(samples_x)) ranks_x[orders_x[i]] <- i

    Could you change this to something like

    wp <- 0;
    for (i in 1:length(samples_x)) {
    if ((samples_x[orders_x[i]] == NA) ||
     (samples_y[orders_x[i]] == NA))
     {
       ranks_x[orders_x[i]] <- NA;
     }
     else
     {
       ranks_x[orders_x[i]] <- wp++;
     }
    }
    

    Then you could either go along later and compress out the NAs, or hope the correlation subroutine just ignores them.

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

Sidebar

Related Questions

I am trying to write an efficient algorithm that will effectively let me merge
I'm trying to write an algorithm that will find the set of all vertices
I have been trying to come up with a way to write an efficient
I'm trying to write more efficient code in a C program, and I need
I am trying write a function that generates simulated data but if the simulated
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write out syslog entries containing strings but they don't register. // person.name
Situation: I am trying to write a efficient query using "LIKE" statement to look
I'm trying to write a small Powershell function that will return some summary data

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.