3
$\begingroup$

What syntax will allow me to find the maximum values, at a given position, across multiple lists of equal length?

For example, suppose we have three lists l1, l2, and l3:

l1 = {1, 2, 1, 1, 5}

l2 = {2, 1, 1, 4, 5}

l3 = {3, 1, 2, 1, 4}

What syntax can be derived to obtain lmax:

lmax = {3, 2, 2, 4, 5}
$\endgroup$
1
  • $\begingroup$ I made a mistake in column 2. That might have been confusing. I edited lmax. $\endgroup$
    – user120911
    Commented Sep 10, 2019 at 11:26

3 Answers 3

3
$\begingroup$

One option is to use MapThread:

MapThread[Max, {l1, l2, l3}]

Or Map (/@) after Transpose:

Max /@ ({l1, l2, l3}\[Transpose])
$\endgroup$
2
$\begingroup$
a = {1, 2, 1, 1, 5};

b = {2, 1, 1, 4, 5};

c = {3, 1, 2, 1, 4};

Using ArrayReduce (new in 12.2)

Columns

ArrayReduce[Max, {a, b, c}, 1]

{3, 2, 2, 4, 5}

Rows

ArrayReduce[Max, {a, b, c}, 2]

{5, 5, 4}

$\endgroup$
2
$\begingroup$
a = {1, 2, 1, 1, 5};
b = {2, 1, 1, 4, 5};
c = {3, 1, 2, 1, 4};

TakeLargest[#, 1] & /@ Transpose@{a, b, c} // Flatten
RankedMax[#, 1] & /@ Transpose[{a, b, c}]

{3, 2, 2, 4, 5}

$\endgroup$

Your Answer

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

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