# Pick the maximum element of each nested list [closed]

Suppose that I have a list with nested lists, where each nested list contain entries consisting in 3 columns:

t = RandomReal[{0, 10}, {60, 3}];

minx = 0;
maxx = 10;
miny = 0;
maxy = 10;
resx = 5;
resy = 5;
stepx = (maxx - minx)/resx;
stepy = (maxy - miny)/resy;
grid = Table[
Select[t,
minx + stepx*(i - 1) < #[[1]] <= minx + stepx* i &&
miny + stepy*(j - 1) < #[[2]] <= miny + stepy*j &], {i, 1,
resx}, {j, 1, resy} ];
Map[MatrixForm, grid, {2}] // MatrixForm


Now I want to generate an array of the same size, in this case $5\times 5$ such that each entry contains the row with the maximum value of the third column of the third list. For instance, in this example such an array MyArray would have as elements:

MyArray[[1,1]]= {1.94367, 1.96706, 5.63112}
MyArray[[1,2]]= {0.706452, 3.92499, 4.00843}
MyArray[[2,1]]= {3.44211, 1.23998, 8.72222}


...

MyArray[[5,5]]= {9.78142, 8.02858, 4.28738}


I know how to pick the maximum element of each sublist, using for instance for the first one:

Pick[grid[[1, 1]], #, Max@#] &@grid[[1, 1]][[All, 3]]


But I don't know how to apply it simultaneously to all the lists. I guess that is using Map somehow, but I cannot figure out how...

Thanks!

• You should post code rather than picture. – xyz Oct 9 '15 at 10:20
• Have a look at Max in doc. – xyz Oct 9 '15 at 10:21
• Since there are 9 nested lists I'd expect nine outputs (one maximum for each nested list). Your output has 27. Please explain. – Sjoerd C. de Vries Oct 9 '15 at 10:21
• You are absolutely right, I hope that now after the editing it is clearer. – Vazquez Oct 9 '15 at 10:37

You just need to map your picking function at the second level of the grid

pick[list_] := Pick[list, #, Max@#] &@list[[All, 3]]
Map[pick, grid, {2}]

data = Table[RandomReal[10, {RandomInteger[{3, 6}], 3}], {i, 3}, {j, 3}];
MatrixForm@Map[MatrixForm, data, {2}]


flatter = Flatten[data, 1];
thirdcols = flatter[[All, All, 3]];
max = Max /@ thirdcols;

• This procedure doesn't work with the grid from the question. – Jack LaVigne Oct 9 '15 at 12:17