4
$\begingroup$

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!

$\endgroup$
5
  • $\begingroup$ Try e.g. {Min[#], Max[#]} & /@ maxtest $\endgroup$ Commented Dec 21, 2020 at 9:52
  • $\begingroup$ 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 $\endgroup$ Commented Dec 21, 2020 at 10:04
  • 3
    $\begingroup$ Transpose[MinMax /@ Transpose@maxtest] $\endgroup$
    – LouisB
    Commented Dec 21, 2020 at 10:06
  • $\begingroup$ Is this what you want? mins = Min /@ maxtest; maxs = Max /@ maxtest $\endgroup$ Commented Dec 21, 2020 at 10:06
  • $\begingroup$ LouisB - Good Thanks this is stright to the solution I will study how you arrive to this fast solution. Thanks again $\endgroup$ Commented Dec 21, 2020 at 10:12

4 Answers 4

5
$\begingroup$

You may use

MapThread[Min, maxtest]
MapThread[Max, maxtest]

{3, 0, 0}

{34, 28, 25}

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

Random`Private`MapThreadMin[maxtest]
Random`Private`MapThreadMax[maxtest]
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Dec 21, 2020 at 10:07
  • $\begingroup$ Sorry I look first your undocumented answer, MapThread[f,list] is perfect. Vielen Danke $\endgroup$ Commented Dec 21, 2020 at 10:27
  • $\begingroup$ Bitte sehr! Gern geschehen. =D $\endgroup$ Commented Dec 21, 2020 at 10:30
3
$\begingroup$
minMax = Thread @* MapThread[MinMax @* List];

minMax @ maxtest
{{3, 0, 0}, {34, 28, 25}}
$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$ Commented Dec 21, 2020 at 11:12
3
$\begingroup$
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}

$\endgroup$
2
$\begingroup$
    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*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.