并行找到两个阵列的最稳定点
Find most stable point of two array parallelly
我有两个数组。
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 index = 8 // this is what i want
索引 8 表示来自 arr1
的“44”和来自 arr2
“78”的“
让我在这里定义 stable
的意思。
至少他们的前一元素和后一元素的差异是两个数组中最小的。 (抱歉我的错误解释。)
不要标记这个问题,只需评论您需要了解的内容,我会尽力解释。 [请]
@Nina Scholz 推荐
这是我到目前为止尝试过的方法[不工作]
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));
您可以取前一个值与实际值、实际值和下一个值的差值,然后取这个差值,得到两个差值的最小差值。
结果和想要的不一样,因为它取了两个数组的最小增量。
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, ' ')));
我有两个数组。
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 index = 8 // this is what i want
索引 8 表示来自 arr1
的“44”和来自 arr2
“78”的“
让我在这里定义 stable
的意思。
至少他们的前一元素和后一元素的差异是两个数组中最小的。 (抱歉我的错误解释。)
不要标记这个问题,只需评论您需要了解的内容,我会尽力解释。 [请]
@Nina Scholz 推荐
这是我到目前为止尝试过的方法[不工作]
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));
您可以取前一个值与实际值、实际值和下一个值的差值,然后取这个差值,得到两个差值的最小差值。
结果和想要的不一样,因为它取了两个数组的最小增量。
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, ' ')));