Virgilio Marone
Virgilio Marone

Reputation: 53

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

?

Upvotes: 5

Views: 874

Answers (3)

panda-34
panda-34

Reputation: 4209

Simplest, though not the fastest:

Inner[Max,data1,data2,List]

Upvotes: 1

sakra
sakra

Reputation: 65781

Another possible solution is to use the MapThread function:

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

belisarius solution however is much faster.

Upvotes: 3

Dr. belisarius
Dr. belisarius

Reputation: 61016

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} *)

Upvotes: 4

Related Questions