2

I'm trying to find a method to find the max value comparing 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 compare the arrays finding the max....?

4
  • don't you mean MAX = [3.3,5.3,5.2]? Nov 25, 2014 at 0:41
  • @Fresheyeball: No, I would like to compare 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
    Nov 25, 2014 at 0:45
  • You should make an array of arrays forwarded to function finding largest numbers. 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
    Nov 25, 2014 at 0:50

6 Answers 6

4

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 compares the current maximum with the next array element and so final always holds the maximum value for all elements traversed thus far.

2
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 ]
1
  • thanks jmfolds! please someone upvote for correct answer
    – James Bund
    Nov 25, 2014 at 2:01
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;
}
1
  • thanks openorclose! please someone upvote for correct answer.
    – James Bund
    Nov 25, 2014 at 2:01
2

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));
1

You can combine 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);
1
  • There's a typo in your answer: because lodash's interface for _.zip changed, you'll have to apply it with _.zip.apply(null, A).
    – APerson
    Aug 10, 2016 at 14:55
0

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);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.