0

I'd like to re-scale a two dimensional array with a function where the min and max input range and min and max output range can be specified. For example, we want to re-scale the values 0 to 8 to 0 to 1.

const scale = (num, in_min, in_max, out_min, out_max) => {
    return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

var array_scaled = orignal_array.map(scale(num, 0, 8, 0, 1));

}

The code produces the following error: ReferenceError: num is not defined

What is the correct syntax to call the scale function from within map?

2 Answers 2

3

i guess the argument of map must be a function, i.e. :

var array_scaled = orignal_array.map(num=>scale(num, 0, 8, 0, 1));
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for solving my syntax error. However, the mapping returns Nan's instead of a new 2 dimensional array. Do you know why?
I ended up doing this: array_scaled= Array.from(Array(n_dimension), () => new Array(m_dimension)); for (var row = 0; row < m_dimension; row++) { for (var col = 0; col < n_dimension; col++) { array_scaled[col][row] = this.scale(original_array[col][row], 0, 8, 0, 1); } }
0

Array.prototype.map() accepts a callback function as its first argument.

It looks like num is not defined as the current element of the array to the callback function scale()

Try var array_scaled = orignal_array.map(array_num => scale(array_num, 0, 8, 0, 1));

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.