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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:08:37+00:00 2026-05-16T22:08:37+00:00

I have a sparse array, for example: rare = [[0,1], [2,3], [4,5], [7,8]] I

  • 0

I have a sparse array, for example:

rare = [[0,1], [2,3], [4,5], [7,8]]

I want to plot a chart with these data, each pair are point coordinates.
As you can see I don’t have points for x=1, x=3 , x=5, x=6

I want to fill the array with the previous values, so for the above example I will get:

filled = [[0,1], [1,1], [2,3], [3,3], [4,5], [5,5], [6,5], [7,8]

As you can see, for calculating the y value, I simply take the last y value I used.

What is the best aproach to accomplish this ?

  • 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-16T22:08:37+00:00Added an answer on May 16, 2026 at 10:08 pm
    Range.new(*rare.transpose.first.sort.values_at(0,-1)).inject([]){|a,i|
      a<<[i, Hash[rare][i] || a.last.last]
    }
    

    Step-by-step explanation:

    1. rare.transpose.first.sort.values_at(0,-1) finds min and max x ([0,7] in your example)
    2. Range.new() makes a range out of it (0..7)
    3. inject iterates through the range and for every x returns pair [x,y], where y is:
      1. y from input array, where defined
      2. y from previously evaluated pair, where not

    Note: here are some other ways of finding min and max x:

    [:min,:max].map{|m| Hash[rare].keys.send m}
    rare.map{|el| el.first}.minmax # Ruby 1.9, by steenslag
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a 2d sparse array. In my real usecase both the number
I have a sparse array (indexes are not consecutive) like this: var testArray =
I have a 3d array containing the non-cubic bounding box of a sparse geometry.
I have a sparse array in Jscript, with non-null elements occuring at both negative
I have a large array 15x15x2200. THis is merely a collection of 15x15 sparse
I have a very sparse Perl matrix (array of arrays), where undef values are
I have a sparse matrix A of very high dimension around 30000x30000 . And
I was wondering if there's any way to have a scipy.sparse.csc_matrix format for mlpy
I have to implement a sparse matrix (a matrix that has predominantly zeroes, so
I have a need for a sparse matrix in up to 4 dimensions in

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.