1
$\begingroup$

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

enter image description here

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!

$\endgroup$

closed as off-topic by xyz, dr.blochwave, MarcoB, m_goldberg, Bob Hanlon Oct 10 '15 at 1:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – xyz, dr.blochwave, MarcoB, m_goldberg, Bob Hanlon
If this question can be reworded to fit the rules in the help center, please edit the question.

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

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}]
$\endgroup$
1
$\begingroup$
data = Table[RandomReal[10, {RandomInteger[{3, 6}], 3}], {i, 3}, {j, 3}];
MatrixForm@Map[MatrixForm, data, {2}]

enter image description here

flatter = Flatten[data, 1];
thirdcols = flatter[[All, All, 3]];
max = Max /@ thirdcols;
rowspositions = Flatten@MapThread[Position, {thirdcols, max}];
extracts = Transpose[{Range[9], rowspositions}];
flatter[[Sequence @@ #]] & /@ extracts

{{9.96146, 4.95039, 9.12134}, {5.0613, 7.55387, 7.6069}, {9.93072, 9.36066, 6.9762}, {5.37254, 5.64206, 7.42148}, {6.2932, 9.29207, 9.24816}, {6.75852, 0.38891, 9.56726}, {1.96899, 7.40202, 6.87286}, {8.00277, 5.72873, 8.91649}, {4.70735, 5.16466, 8.67289}}

$\endgroup$
  • $\begingroup$ This procedure doesn't work with the grid from the question. $\endgroup$ – Jack LaVigne Oct 9 '15 at 12:17
  • $\begingroup$ The OP changed the question after I posted my answer. $\endgroup$ – Chris Degnen Oct 9 '15 at 13:31

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