Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element-wise maximum value for two lists

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

?

like image 843
Virgilio Marone Avatar asked Jul 12 '13 15:07

Virgilio Marone


People also ask

How do you find the max value of a multiple list in Python?

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?

What is element wise Max?

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.

How do you find the minimum value of multiple lists in Python?

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.


3 Answers

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} *)
like image 171
Dr. belisarius Avatar answered Sep 27 '22 23:09

Dr. belisarius


Another possible solution is to use the MapThread function:

data3 = MapThread[Max, {data1, data2}]

belisarius solution however is much faster.

like image 22
sakra Avatar answered Sep 27 '22 22:09

sakra


Simplest, though not the fastest:

Inner[Max,data1,data2,List]
like image 35
panda-34 Avatar answered Sep 28 '22 00:09

panda-34