Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find most stable point of two array parallelly

I have two arrays.

let arr1 = [32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50];
let arr2 = [46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97];

Now I want a "stable" index of both arrays. like what is the index when both arrays are most state?

For example from the given array I want.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let index = 8 // this is what i want

Index 8 means from arr1 the ist "44" and from arr2 "78"

Let me define what I mean by stable here.

At least their one before element and one after element’s difference is the least of both arrays. (Sorry for my bad explanation.)

Don’t flag this question just comment on what you need to know, I will try to explain. [Please]

As recommended by @Nina Scholz

This is what i tried so far [not working]

let arr1 = [32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50];
let arr2 = [46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97];
let diff_arr1 = []
let diff_arr2 = []
let compare_arr = []

for (let i = 0; i < arr1.length; i++) {
    diff_arr1.push(Math.abs(arr1[i + 1] - arr1[i]));
}

for (let i = 0; i < arr2.length; i++) {
    diff_arr2.push(Math.abs(arr2[i + 1] - arr2[i]));
}

for (let i = 0; i < 12; i++) {
    compare_arr.push(Math.abs(diff_arr2[i] - diff_arr1[i]));
}

console.log("Array 1")
console.log("Array 1 diff");
console.log(diff_arr1);

console.log("Array 2")
console.log("Array 2 diff");
console.log(diff_arr2);

console.log('compare_arr')
console.log(compare_arr)
let arr_temp = compare_arr.filter(function (value) {
    return !Number.isNaN(value);
});
console.log(Math.min.apply(Math, arr_temp));

>Solution :

You could take the delta of the previous value with actual and actual and next value and take later this delta the get a minimum delta of both deltas.

The result is different from the wanted, because it take the minimum delta of both arrays.

                          v
 32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50
NaN  13  22  15   8   8   7   6   3   5   5 NaN

 46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97
NaN   9   1   2   8  13   7   8  14   8  12 NaN
const
    getDelta = (array, offset) => array
        .map((v, i, a) => Math.abs(v - array[i - 1]) + Math.abs(v - array[i + 1])),
    array1 = [32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50],
    array2 = [46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97],
    delta1 = getDelta(array1, 2),
    delta2 = getDelta(array2, 2),
    result = [...array1.keys()]
        .slice(1, -1)
        .reduce((a, b) => Math.abs(delta1[a] - delta2[a]) < Math.abs(delta1[b] - delta2[b])
        ? a
        : b
    );

console.log(result);
console.log(...delta1.map(v => v.toString().padStart(2, ' ')));
console.log(...delta2.map(v => v.toString().padStart(2, ' ')));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading