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 ?
Step-by-step explanation:
rare.transpose.first.sort.values_at(0,-1)finds min and maxx([0,7]in your example)Range.new()makes a range out of it (0..7)injectiterates through the range and for everyxreturns pair[x,y], whereyis:yfrom input array, where definedyfrom previously evaluated pair, where notNote: here are some other ways of finding min and max x: