0

I have an arrays of 5 values containing example:

0.01
0.1
1
10
100

0.1
1
10
100
1000

1
10
100
1000
10000

5
50
500
5000
50000

I need to convert to arrays holding 100 values spaced in same proportion and containing as well the original 5 values in the sequence. Like:

[0.01,...,0.1,...1,...10,...100]

I thought I could use numeric.linspace(0.01,100,100); http://www.numericjs.com/documentation.html

But this only passes first and last wanted value. Ideally some function that accepts an array of n values as first parameter and the final wanted array.length

I was not sure how to formulate the question so please suggest a different title if more appropriated...

2
  • Can you explain using input and output? Like if I input 5 or 0.1 what should be the output? Commented Jan 19, 2017 at 9:50
  • 1
    So sorry guys does not make sense what I posted...obviously it is not possible to maintain even spaced proportions and original values in between min and max at the same time..... Commented Jan 19, 2017 at 10:00

2 Answers 2

1

Hope this is what you meant:

function expand(arr, len){
  var perSection = Math.floor(len / (arr.length - 1));
  var out = [];
  var val;
  console.log('Per Section:', perSection);
  
  for (let i = 0; i < arr.length - 1; i++) {
  	val = arr[i];
    out.push(val);
    var interval = (arr[i+1] - arr[i]) / perSection;
    console.log('ival', interval);
  	for (let j = 0; j < perSection-1; j++) {
    	val += interval;
    	out.push(val)
    }
  }
  out.push(arr[arr.length-1]);
  
  return out;
}

console.log(expand([10, 40, 70, 100], 10));

Note that the length of the resulting array will not always match exactly to the requested length, as there might be rounding errors when calculating how many numbers go into each interval.
Depending on the required behavior additional logic can be added to handle these edge cases.

Sign up to request clarification or add additional context in comments.

Comments

1

You could map the values with a weight.

var space = [0.01, 0.1, 1, 10, 100],
    value = 7,
    values = space.map(function (a, i, aa) {
        this.last = i ? this.last * a / aa[i - 1] : value;
        return this.last;
    }, { });

console.log(values);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.