Given two Mathematica sets of data such as
data1 = {0, 1, 3, 4, 8, 9, 15, 6, 5, 2, 0};
data2 = {0, 1, 2, 5, 8, 7, 16, 5, 5, 2, 1};
how can I create a set giving me the maximum value of the two lists, i.e. how to obtain
data3 = {0, 1, 3, 5, 8, 9, 16, 6, 5, 2, 1};
?
With the key argument of the max() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the largest numerical value is returned as the maximum of the list of lists. How to Find the Max of List of Lists in Python?
maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned.
Using min() and zip() In the below example we use the min() and zip(). Here the zip() function organizes the elements at the same index from multiple lists into a single list. Then we apply the min() function to the result of zip function using a for loop.
data1 = {0, 1, 3, 4, 8, 9, 15, 6, 5, 2, 0};
data2 = {0, 1, 2, 5, 8, 7, 16, 5, 5, 2, 1};
Max /@ Transpose[{data1, data2}]
(* {0, 1, 3, 5, 8, 9, 16, 6, 5, 2, 1} *)
Another possible solution is to use the MapThread function:
data3 = MapThread[Max, {data1, data2}]
belisarius solution however is much faster.
Simplest, though not the fastest:
Inner[Max,data1,data2,List]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With