快速查找矩阵第 N 列中 max/min 元素索引的方法
Fast way to find index of max/min element in Nth column of matrix
我正在尝试获取矩阵中特定列的最大和最小元素的索引。现在我正在使用 ES6 和 Spread 语法进行如下操作:
a = [
[22,23],
[74,1],
[21,33],
[32,84],
[11,31],
[1,49],
[7,8],
[11,11],
[99,68],
[52,20]
];
const minValue = (arr, n) => Math.min(...arr.map(x => x[n])); //n - column index
const maxValue = (arr, n) => Math.max(...arr.map(x => x[n]));
const minValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(minValue(arr, n));
const maxValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(maxValue(arr, n));
console.log(minValue(a, 0));
console.log(maxValue(a, 0));
console.log(minValueIndex(a, 0));
console.log(maxValueIndex(a, 0));
但同时进行了太多的比较,我认为这不是完成这项任务的最佳方式。我很高兴看到使用或不使用 ES6 标准的更快实现。
你快到了,你只是关心性能对吧?因此,为了提高程序的性能,您可以使用一种称为 Memoization
的好方法
Memoization is an optimization technique used primarily to speed up
computer programs by storing the results of expensive function calls
and returning the cached result when the same inputs occur again
const arr = [[22,23], [74,1], [21,33], [32,84], [11,31], [1,49], [7,8], [11,11], [99,68], [52,20]];
/**
* Here I create the momoized function which cache the
* column and if we want to get the same column then it
* simply return the previously cached column array
* otherwise, it get the column and cache it for future
* and return it.
*/
const memoized = () => {
const cache = {};
return (arr, index) => {
if (index in cache) {
return cache[index];
} else {
const col = arr.map(item => (item[index]));
cache[index] = col;
return col;
}
}
}
/**
* As memoized is a higher order function so it returns
* another function which will be executed by calling
* this getColumn function reference.
*/
const getColumn = memoized();
const getMinValue = (arr, col) => Math.min(...getColumn(arr, col));
const getMaxValue = (arr, col) => Math.max(...getColumn(arr, col));
const minValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMinValue(arr, col));
const maxValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMaxValue(arr, col));
console.log('minValue: ', getMinValue(arr, 0)); // Calculated
console.log('maxValue: ',getMaxValue(arr, 0)); // Cached
console.log('minValueIndex: ', minValueIndex(arr, 0)); // Cached
console.log('maxValueIndex: ', maxValueIndex(arr, 0)); // Cached
.as-console-wrapper {min-height: 100% !important; top: 0;}
一个简单的解决方案是遍历数组并将 min/max 值存储在临时变量中。
function minMax (arr, n) {
let min=Infinity, max=0;
for (const _arr of arr) {
const x = _arr[n];
if (x < min) min = x;
if (x > max) max = x;
}
return [min, max];
}
function minMaxIndex (arr, n) {
let min=Infinity, max=0, minIndex, maxIndex;
for (let i=0; i < arr.length; i++) {
const x = arr[i][n];
if (x < min) {
min = x;
minIndex = i;
}
if (x > max) {
max = x;
maxIndex = i;
}
}
return [minIndex, maxIndex];
}
console.log (minMax(a, 0))
console.log (minMaxIndex(a, 0))
<script>
a = [
[22,23],
[74,1],
[21,33],
[32,84],
[11,31],
[1,49],
[7,8],
[11,11],
[99,68],
[52,20]
];
</script>
Will this help?
let a = [
[22, 23],
[74, 1],
[21, 33],
[32, 84],
[11, 31],
[1, 49],
[7, 8],
[11, 11],
[99, 68],
[52, 20]
];
let max = 0,
min = 0,
minIndex = 0,
maxIndex = 0;
const findValue = (array, col) => {
array.map((matrix) => {
(matrix[col] > max) ? max = matrix[col]: null;
(min == 0) ? min = max: null;
(matrix[col] < min) ? min = matrix[col]: null;
})
}
const findIndex = (array, col, min, max) => {
minIndex = array.map(data => data[col]).indexOf(min);
maxIndex = array.map(data => data[col]).indexOf(max);
}
findValue(a, 0)
findIndex(a, 0, min, max);
console.log(min, max, minIndex, maxIndex);
我正在尝试获取矩阵中特定列的最大和最小元素的索引。现在我正在使用 ES6 和 Spread 语法进行如下操作:
a = [
[22,23],
[74,1],
[21,33],
[32,84],
[11,31],
[1,49],
[7,8],
[11,11],
[99,68],
[52,20]
];
const minValue = (arr, n) => Math.min(...arr.map(x => x[n])); //n - column index
const maxValue = (arr, n) => Math.max(...arr.map(x => x[n]));
const minValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(minValue(arr, n));
const maxValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(maxValue(arr, n));
console.log(minValue(a, 0));
console.log(maxValue(a, 0));
console.log(minValueIndex(a, 0));
console.log(maxValueIndex(a, 0));
但同时进行了太多的比较,我认为这不是完成这项任务的最佳方式。我很高兴看到使用或不使用 ES6 标准的更快实现。
你快到了,你只是关心性能对吧?因此,为了提高程序的性能,您可以使用一种称为 Memoization
Memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again
const arr = [[22,23], [74,1], [21,33], [32,84], [11,31], [1,49], [7,8], [11,11], [99,68], [52,20]];
/**
* Here I create the momoized function which cache the
* column and if we want to get the same column then it
* simply return the previously cached column array
* otherwise, it get the column and cache it for future
* and return it.
*/
const memoized = () => {
const cache = {};
return (arr, index) => {
if (index in cache) {
return cache[index];
} else {
const col = arr.map(item => (item[index]));
cache[index] = col;
return col;
}
}
}
/**
* As memoized is a higher order function so it returns
* another function which will be executed by calling
* this getColumn function reference.
*/
const getColumn = memoized();
const getMinValue = (arr, col) => Math.min(...getColumn(arr, col));
const getMaxValue = (arr, col) => Math.max(...getColumn(arr, col));
const minValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMinValue(arr, col));
const maxValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMaxValue(arr, col));
console.log('minValue: ', getMinValue(arr, 0)); // Calculated
console.log('maxValue: ',getMaxValue(arr, 0)); // Cached
console.log('minValueIndex: ', minValueIndex(arr, 0)); // Cached
console.log('maxValueIndex: ', maxValueIndex(arr, 0)); // Cached
.as-console-wrapper {min-height: 100% !important; top: 0;}
一个简单的解决方案是遍历数组并将 min/max 值存储在临时变量中。
function minMax (arr, n) {
let min=Infinity, max=0;
for (const _arr of arr) {
const x = _arr[n];
if (x < min) min = x;
if (x > max) max = x;
}
return [min, max];
}
function minMaxIndex (arr, n) {
let min=Infinity, max=0, minIndex, maxIndex;
for (let i=0; i < arr.length; i++) {
const x = arr[i][n];
if (x < min) {
min = x;
minIndex = i;
}
if (x > max) {
max = x;
maxIndex = i;
}
}
return [minIndex, maxIndex];
}
console.log (minMax(a, 0))
console.log (minMaxIndex(a, 0))
<script>
a = [
[22,23],
[74,1],
[21,33],
[32,84],
[11,31],
[1,49],
[7,8],
[11,11],
[99,68],
[52,20]
];
</script>
Will this help?
let a = [
[22, 23],
[74, 1],
[21, 33],
[32, 84],
[11, 31],
[1, 49],
[7, 8],
[11, 11],
[99, 68],
[52, 20]
];
let max = 0,
min = 0,
minIndex = 0,
maxIndex = 0;
const findValue = (array, col) => {
array.map((matrix) => {
(matrix[col] > max) ? max = matrix[col]: null;
(min == 0) ? min = max: null;
(matrix[col] < min) ? min = matrix[col]: null;
})
}
const findIndex = (array, col, min, max) => {
minIndex = array.map(data => data[col]).indexOf(min);
maxIndex = array.map(data => data[col]).indexOf(max);
}
findValue(a, 0)
findIndex(a, 0, min, max);
console.log(min, max, minIndex, maxIndex);