# Applying a function to every component of a list of vectors

I tried to find Max or Min to lists of lists, with Map with Apply ... but the only way has been Module ... maybe someone can show us a direct more efficient way

this is the source of data:

maxtest ={{7, 2, 5}, {7, 8, 1}, {20, 1, 19}, {8, 23, 15}, {26, 27, 1}, {13, 17,
4}, {3, 25, 22}, {28, 4, 24}, {25, 14, 11}, {24, 28, 4}, {25, 0,
25}, {11, 7, 4}, {28, 20, 8}, {6, 25, 19}, {34, 12, 22}, {25, 9,
16}, {6, 19, 13}, {10, 15, 5}, {7, 7, 0}, {23, 28, 5}}


and this is the weak solution I wrote

maxlist[lst_] := Module[{dim, i},
dim = Dimensions[lst[[2]]][[1]];
resp = Table[0, dim];
For[i = 1, i <= dim, i++,
resp[[i]] = Max[lst[[All, i]]]];
Return [resp]
]


giving me

In[17]:= maxlist[maxtest]

Out[17]= {34, 28, 25}


thanks for help!

• Try e.g. {Min[#], Max[#]} & /@ maxtest Dec 21, 2020 at 9:52
• Thanks Daniel, but this gives me a list of lists where each term has tha min and the max of every term. I pretend to have the min of all list[[1]] the min of all[2]] and so on for any dimension of the elements Dec 21, 2020 at 10:04
• Transpose[MinMax /@ Transpose@maxtest] Dec 21, 2020 at 10:06
• Is this what you want? mins = Min /@ maxtest; maxs = Max /@ maxtest Dec 21, 2020 at 10:06
• LouisB - Good Thanks this is stright to the solution I will study how you arrive to this fast solution. Thanks again Dec 21, 2020 at 10:12

You may use

MapThread[Min, maxtest]


{3, 0, 0}

{34, 28, 25}

And here are two nice undocumented functions that do the same job very efficiently:

RandomPrivateMapThreadMin[maxtest]
RandomPrivateMapThreadMax[maxtest]

• Thanks Henrik Schumacher, that's it, ok it's a function in the dark , well in M11.2 I had to write the procedural way. Dec 21, 2020 at 10:07
• Sorry I look first your undocumented answer, MapThread[f,list] is perfect. Vielen Danke Dec 21, 2020 at 10:27
• Bitte sehr! Gern geschehen. =D Dec 21, 2020 at 10:30
minMax = Thread @* MapThread[MinMax @* List];

minMax @ maxtest

{{3, 0, 0}, {34, 28, 25}}

• Thanks for your answer, I checked it and of course works. I never used @* together, I would study how it works and try to get profit for next opportunity Dec 21, 2020 at 11:12
m =
{{7, 2, 5}, {7, 8, 1}, {20, 1, 19}, {8, 23, 15}, {26, 27, 1},
{13, 17, 4}, {3, 25, 22}, {28, 4, 24}, {25, 14, 11}, {24, 28, 4},
{25, 0, 25}, {11, 7, 4}, {28, 20, 8}, {6, 25, 19}, {34, 12, 22},
{25, 9, 16}, {6, 19, 13}, {10, 15, 5}, {7, 7, 0}, {23, 28, 5}};


Using ArrayReduce (new in 12.2)

ArrayReduce[#, m, 1] & /@ {Min, Max, MinMax}


{{3, 0, 0}, {34, 28, 25}, {{3, 34}, {0, 28}, {0, 25}}}

To apply a function to matrix rows we use 2 as last parameter

ArrayReduce[Min, list, 2]


{2, 1, 1, 8, 1, 4, 3, 4, 11, 4, 0, 4, 8, 6, 12, 9, 6, 5, 0, 5}

    m =
{{7, 2, 5}, {7, 8, 1}, {20, 1, 19}, {8, 23, 15}, {26, 27, 1},
{13, 17, 4}, {3, 25, 22}, {28, 4, 24}, {25, 14, 11}, {24, 28, 4},
{25, 0, 25}, {11, 7, 4}, {28, 20, 8}, {6, 25, 19}, {34, 12, 22},
{25, 9, 16}, {6, 19, 13}, {10, 15, 5}, {7, 7, 0}, {23, 28, 5}}


Using Thread:

minMax = Thread[MinMax /@ Thread@#] &;

minMax@m

(*{{3, 0, 0}, {34, 28, 25}}*)


To apply a function to matrix rows, we proceed as follows:

Thread[Min /@ m] === ArrayReduce[Min, m, 2]

(*True*)