# Find highest values between two different sets

I have the two following lists:

a1 = {0, 2, 4, 7, 8, 10, 11, 13};
b1 = {1, 4, 15, 8, 6, 2, 2, 1};
c1 = Thread@{a1, b1};
a2 = {0, 1, 4, 6, 8, 10, 13};
b2 = {1, 6, 12, 10, 4, 1, 1};
c2 = Thread@{a2, b2};


(a1 and a2 start and end with the same values, their lengths can be different)

producing the following figure with ListPlot[{c1, c2}, Joined -> True, Mesh -> All]:

I would like to find the enveloped points of the two lists, so I tried:

a3 = Join[a1, a2] // DeleteDuplicates // Sort;
f[l_List] := Interpolation[l, InterpolationOrder -> 1, Method -> "Spline"]
b11 = f[c1][#] & /@ a3;
b22 = f[c2][#] & /@ a3;
env = Table[Max[#[[i]] & /@ {b22, b11}], {i, 1, Length@a3}];
ListPlot[{c1, c2, Thread@{a3, env}}, Joined -> True, Mesh -> All]


But it's not really as good as what I was hoping for.

I'm a little bit rusty, so I know that you all know a better way and correct way to do this :)

• Are the two lists guaranteed to be of the same length? – Shredderroy Oct 9 at 20:37
• No they are not. I have a list with 20ish values, and an other one with 62 values. I should edit the question with lists with various lengths, thank you. – Öskå Oct 9 at 20:38
• The only guarantee is that they start and end with the same values for ai. – Öskå Oct 9 at 20:40

## 1 Answer

You can use TemporalData

td = TemporalData[{c1, c2},
ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}];

upperEnvelope = Max @ Through[td["PathFunctions"] @ #] &;

plt1 = ListLinePlot[{c1, c2}, Mesh -> All, ImageSize -> 400];
plt2 = Plot[upperEnvelope @ t, {t, 0, 10},
PlotStyle -> Directive[Opacity[.5], Red, CapForm["Round"], Thickness[.02]]];

Row[{plt1, Show[plt1, plt2, ImageSize -> 400]}, Spacer[10]]


Use Mesh -> {Union[a1, b1]} to get

Alternatively, you can post-process plt1 to construct an interpolation function for each line and define the upper envelope as the maximum of those functions:

iFs = Cases[Normal @ plt1, Line[x_]:> Interpolation[x, InterpolationOrder->1], All];
upperEnvelope2 = Max @ Through @ iFs @ #&;

• Instead of plotting with Plot, I would go with Row[{plt1, ListPlot[{c1, c2, Thread@{a3, upperEnvelope /@ a3}}, Joined -> True, Mesh -> All, ImageSize -> 400]}] which shows a little bit more. Your solution works like a charm. – Öskå Oct 9 at 21:08