1
$\begingroup$

I have some data and the rows are grouped according to astGroupsPos (the astGroupsPos are just rows from dat ordered and grouped in some fashion):

dat = {{0.148`, 0.014`, 0.`, 0.`, 0.035`, 0.`, 0.`, 0.735`, 0.065`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.077`, 0.`, 
    0.019`, 0.`, 0.012`, 0.`, 0.263`, 0.612`, 0.018`, 0.`, 0.`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.063`, 0.`, 0.017`, 0.`, 0.027`, 
    0.`, 0.216`, 0.66`, 0.016`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`, 0.`}, {0.231`, 0.075`, 0.009`, 0.`, 0.009`, 0.002`, 0.266`, 
    0.404`, 0.006`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.17099999999999999`, 0.098`, 0.01`, 0.`, 0.006`, 0.002`, 
    0.335`, 0.374`, 0.007`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.151`, 0.083`, 0.012`, 0.`, 0.018`, 0.`, 0.503`, 0.222`, 
    0.011`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.173`, 
    0.056`, 0.`, 0.`, 0.018`, 0.`, 0.491`, 0.25`, 0.014`, 0.`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.131`, 0.069`, 0.012`, 0.`, 
    0.019`, 0.`, 0.479`, 0.267`, 0.023`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`,
     0.`, 0.`, 0.`}, {0.15100000000000002`, 0.045`, 0.`, 0.`, 0.014`, 
    0.`, 0.268`, 0.508`, 0.013`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`, 0.`}, {0.14600000000000002`, 0.039`, 0.011`, 0.`, 0.023`, 
    0.`, 0.434`, 0.324`, 0.022`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`, 0.`}, {0.059`, 0.`, 0.02`, 0.`, 0.023`, 0.`, 0.202`, 0.643`, 
    0.053`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.11499999999999999`, 0.049`, 0.01`, 0.008`, 0.03`, 0.`, 
    0.233`, 0.541`, 0.02`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.111`, 0.014`, 0.017`, 0.`, 0.022`, 0.`, 0.208`, 0.613`, 
    0.015`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.078`, 
    0.038`, 0.011`, 0.022`, 0.054`, 0.`, 0.299`, 0.475`, 0.024`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.098`, 0.034`, 0.04`, 
    0.019`, 0.016`, 0.`, 0.371`, 0.407`, 0.016`, 0.`, 0.`, 0.`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`}, {0.131`, 0.067`, 0.03`, 0.`, 0.029`, 
    0.`, 0.29`, 0.428`, 0.019`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`, 0.`}, {0.152`, 0.049`, 0.01`, 0.037`, 0.031`, 0.`, 0.277`, 
    0.417`, 0.019`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.123`, 0.065`, 0.039`, 0.093`, 0.009`, 0.`, 0.167`, 
    0.438`, 0.019`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.128`, 0.086`, 0.034`, 0.`, 0.016`, 0.`, 0.102`, 0.577`, 
    0.003`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.097`, 
    0.142`, 0.011`, 0.`, 0.017`, 0.002`, 0.244`, 0.428`, 0.022`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.131`, 0.048`, 0.028`, 
    0.`, 0.015`, 0.`, 0.212`, 0.533`, 0.025`, 0.`, 0.`, 0.`, 0.`, 0.`,
     0.`, 0.`, 0.`, 0.`}, {0.106`, 0.232`, 0.034`, 0.`, 0.022`, 0.`, 
    0.304`, 0.259`, 0.04`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.116`, 0.085`, 0.042`, 0.`, 0.016`, 0.004`, 0.15`, 0.567`,
     0.013`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.033`, 
    0.032`, 0.`, 0.`, 0.022`, 0.`, 0.`, 0.828`, 0.084`, 0.`, 0.`, 0.`,
     0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.122`, 0.104`, 0.009`, 0.`, 
    0.014`, 0.`, 0.216`, 0.517`, 0.018`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`,
     0.`, 0.`, 0.`}, {0.087`, 0.007`, 0.012`, 0.`, 0.01`, 0.`, 0.242`,
     0.624`, 0.017`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.145`, 0.021`, 0.008`, 0.`, 0.039`, 0.`, 0.`, 0.745`, 
    0.052`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.119`, 
    0.029`, 0.017`, 0.`, 0.116`, 0.`, 0.114`, 0.551`, 0.045`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.052`, 0.022`, 0.057`, 
    0.`, 0.093`, 0.`, 0.095`, 0.579`, 0.1`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.`, 0.`, 0.`, 0.`}, {0.265`, 0.316`, 0.`, 0.`, 0.047`, 0.036`, 
    0.`, 0.153`, 0.02`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.185`}, {0.376`, 0.368`, 0.`, 0.`, 0.032`, 0.062`, 0.`, 0.088`, 
    0.07`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.078`}, {0.437`, 
    0.283`, 0.`, 0.`, 0.034`, 0.071`, 0.`, 0.071`, 0.04`, 0.`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.108`}, {0.401`, 0.33`, 0.`, 0.`, 
    0.071`, 0.027`, 0.`, 0.114`, 0.062`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`,
     0.`, 0.`, 0.058`}, {0.267`, 0.346`, 0.`, 0.`, 0.044`, 0.036`, 
    0.`, 0.168`, 0.062`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.14`}, {0.122`, 0.084`, 0.`, 0.`, 0.061`, 0.05`, 0.`, 0.6`, 
    0.109`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`}, {0.34`, 
    0.19`, 0.`, 0.`, 0.059`, 0.002`, 0.`, 0.222`, 
    0.034999999999999996`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 
    0.133`}, {0.367`, 0.359`, 0.`, 0.`, 0.053`, 0.056`, 0.`, 0.015`, 
    0.039`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.141`}, {0.369`, 
    0.301`, 0.`, 0.`, 0.035`, 0.042`, 0.`, 0.013`, 0.062`, 0.`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.24`}, {0.816`, 0.`, 0.`, 0.`, 0.`,
     0.002`, 0.`, 0.`, 0.003`, 0.009`, 0.111`, 0.059`, 0.`, 0.`, 0.`, 
    0.`, 0.`, 0.`}, {0.116`, 0.`, 0.011`, 0.`, 0.`, 0.`, 0.585`, 
    0.228`, 0.004`, 0.`, 0.005`, 0.022`, 0.029`, 0.`, 0.`, 0.`, 0.`, 
    0.`}, {0.079`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.`, 0.603`, 0.083`, 0.`, 
    0.006`, 0.`, 0.085`, 0.144`, 0.`, 0.`, 0.`, 0.`}, {0.072`, 0.`, 
    0.`, 0.`, 0.`, 0.`, 0.`, 0.715`, 0.097`, 0.`, 0.`, 0.`, 0.045`, 
    0.`, 0.021`, 0.05`, 0.`, 0.`}};
astGroupsPos = {{41}, {1}, {42}, {2, 3}, {4, 5, 6, 7, 8, 9, 10, 11, 
    12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
    40}, {26}, {27, 28}, {29}, {30, 31, 32, 33, 34, 35, 36}, {37, 
    38}, {39}};

I can then group the data accordingly by:

(data[[#]] & /@ astGroupsPos)

So that I get median of each group (grouped rows) like:

(Median /@ (data[[#]] & /@ astGroupsPos))

I would like to do the same for Max and Min but the problem is that they flatten the array first. Is there any nice way to do it without loops? To be more explicit, from:

(data[[#]] & /@ astGroupsPos)[[4]]

the maxima are an array {Max[{0.077,0.063}],Max[{0.,0.}],Max[{0.019,0.017}],... and so on. I found a related post here but this structure seems a bit more complicated

$\endgroup$
  • 1
    $\begingroup$ Transpose[Max /@ Transpose[data]] should yield a columnwise maximum. $\endgroup$ – J. M. will be back soon Feb 16 '16 at 0:23
  • $\begingroup$ @J.M. This does not work due to the more complicated structure of the data, I do not want to apply the Max to data but rather to (data[[#]] & /@ astGroupsPos) which cannot be actually transposed. $\endgroup$ – leosenko Feb 16 '16 at 0:46
  • 1
    $\begingroup$ Do you want what J.M. said, just mapped over your groupings? Max /@ Transpose@# & /@ (data[[#]] & /@ astGroupsPos) $\endgroup$ – wxffles Feb 16 '16 at 1:01
  • $\begingroup$ Yes, that does the job, thank you. Can you put it as the answer? $\endgroup$ – leosenko Feb 16 '16 at 1:11
  • 1
    $\begingroup$ Another way to do @wxffles's generalization: (Map[Max]@*Transpose) /@ (data[[#]] & /@ astGroupsPos) $\endgroup$ – J. M. will be back soon Feb 16 '16 at 1:23
4
$\begingroup$

Here's a way not mentioned in the comments, using Flatten to do a ragged transpose as shown in the "Applications" section of the documentation.

Map[
 Max,
 Flatten[
  dat[[#]] & /@ astGroupsPos,
  {{1}, {3}, {2}}],
 {2}]

Max may be replaced by Min, Median or other function.

$\endgroup$
  • 1
    $\begingroup$ Anytime anyone uses the ragged transpose application of Flatten, it's an automatic +1. $\endgroup$ – kale Feb 16 '16 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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