最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find max value comparing multiple arrays for each index - Stack Overflow

programmeradmin2浏览0评论

I'm trying to find a method to find the max value paring multiple(unknown number, but same length) arrays for each observation in the arrays, returning an array with the max values.

Example:

EDIT:

 A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2], etc......]

Returning

MAX = [2.2, 5.3, 5.2]

Able to check that the 'input'-arrays are of the same length, but not able to pare the arrays finding the max....?

I'm trying to find a method to find the max value paring multiple(unknown number, but same length) arrays for each observation in the arrays, returning an array with the max values.

Example:

EDIT:

 A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2], etc......]

Returning

MAX = [2.2, 5.3, 5.2]

Able to check that the 'input'-arrays are of the same length, but not able to pare the arrays finding the max....?

Share Improve this question edited Nov 25, 2014 at 1:51 James Bund asked Nov 25, 2014 at 0:39 James BundJames Bund 1932 silver badges14 bronze badges 4
  • don't you mean MAX = [3.3,5.3,5.2]? – Fresheyeball Commented Nov 25, 2014 at 0:41
  • @Fresheyeball: No, I would like to pare A[0] with B[0] with C[0].. and A[1] with B[1] with C[1] etc.. Not to find the max value in each array, but across the arrays for 0:length-1... – James Bund Commented Nov 25, 2014 at 0:45
  • You should make an array of arrays forwarded to function finding largest numbers. – Daniel Kucal Commented Nov 25, 2014 at 0:45
  • @DiKey: Kind of what i'm not able to do.. Are you able to give an example? – James Bund Commented Nov 25, 2014 at 0:50
Add a ment  | 

6 Answers 6

Reset to default 5

You could use Array.reduce():

    var A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2]];
    
    var max = A.reduce(function(final, current) {
      for (var i = 0; i < final.length; ++i) {
        if (current[i] > final[i]) {
          final[i] = current[i];
        }
      }
      return final;
    });
    
    console.log(max);

The inner function pares the current maximum with the next array element and so final always holds the maximum value for all elements traversed thus far.

var data = [
    [2.2, 3.3, 1.3],
    [1.2, 5.3, 2.2],
    [0.3, 2.2, 5.2]
];

function maxAtIndex (data) {
    //output
    var maxArray = [];
    //loop arrays passed in
    for (var i = 0; i < data[0].length; i++) {
        var possibleValues = [];
        //get value in array at index
        for (var j = 0; j < data.length; j++) {
            possibleValues.push(data[j][i]);
        }
        //get the highest from possible values
        var highest = Math.max.apply(null, possibleValues);
        //store in output array
        maxArray.push(highest);
    }
    return maxArray;
};

console.log(maxAtIndex(data)); //[ 2.2, 5.3, 5.2 ]

For each index of the array, create an array containing all elements in the "column" and find the max of those values. Return the generated array. Sample usage: maxValues(A) would give the desired result.

function maxValues(array) {
    var maxArray = [];
    var length = array[0].length;
    for (var i = 0; i < length; i++) {
        var ithColumn = [].map.call(array, function(array) {
            return array[i];
        });
        maxArray.push(Math.max.apply(null, ithColumn));
    }
    return maxArray;
}

Here's a short and sweet version:

var A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2]];

var maxA = A.map(a => Math.max.apply(null, a));

You can bine Lo-Dash's zip and map methods to do this in just a few lines of code:

var A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2]];

// Creates an array of arrays, where the first array is all the first elements,
// the second array is all the second elements, etc.
var zipped = _.zip(A);
var maxes = _.map(zipped, function(arr) {
    return _.max(arr);
});
console.log(maxes);

I took Tom Panning's answer and simplified it even further:

var A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2]];
var MAX = _.zip.apply(null, A).map(_.max);
发布评论

评论列表(0)

  1. 暂无评论