mljs / array-utils

Some array utilities in JavaScript
MIT License
2 stars 1 forks source link

Create a scale method #5

Open lpatiny opened 8 years ago

lpatiny commented 8 years ago

could be inplace or as a new array

I think we should not throw an error

function scale(array, min, max) {

    var minMax=ML.Stat.array.minMax(array);
if (minMax.max===minMax.min) throw new Error('normalize: max must be different from in');
    var slope=(max-min)/(minMax.max-minMax.min);
    console.log(slope);
    for (var i=0; i<array.length; i++) {
        array[i]=(array[i]-minMax.min)*slope+min;
    }
    console.log(array);
    return array;
}
targos commented 8 years ago

i zlthink it should be scale(array, options). Min and max can have default values

lpatiny commented 8 years ago

min:0, max:1 ?

andcastillo commented 8 years ago

No default values. If only min is defined the array will be multiplied simply by min/Stat.min(input) and if only max is specified then max/Stat.max(input). If both are specified then:

var minMax = Stat.minMax(input);
var factor = (max - min)/(minMax.max-minMax.min);
for(var i=0;i< y.length;i++){
    y[i]=(input[i]-minMax.min)*factor+min;
}

The method also receive the inplace option, by default false

lpatiny commented 8 years ago

Don't know which behaviour is the more logical. I was expecting: You specify nothing : min = Stat.min, max=Stat.max -> we do nothing You specify min : min = newMin, max=Stat.max You specify max : min = Stat.min, max=newMax You specify min and max : min = newMin, max=newMax But I don't know what is the "usual" behaviour in some other software

targos commented 8 years ago

about the option, should we use camelCase ? => inPlace instead of inplace

andcastillo commented 8 years ago

inPlace change was made. About the other problem, I would prefer that if you don't specify min or max the 0 be maintained. So, it is about what you prefer to keep, your baseline or your unspecified value.

lpatiny commented 8 years ago

OK I guess both are useful ... What about other programs ? They allow both ? what could be the option name ?