The question of taking (or applying a transformation to) some part of a rectangular array based on the ordering of another part has come up in many variations. It is related to the general concept of Concomitants of order statistics
(see, for example, Yang).
Using the built-in function Ordering
(as in Gabriel's answer), one can define a function to extract the concomitants in a dataset as follows:
concomitantRows[list_, cols_, ordcol_, ranks_, orderingF_: Less] :=
Part[list[[All, cols]], Ordering[list[[All, ordcol]], ranks, orderingF]]
which takes a list
, and extracts cols
associated with the ranks
in the ordcol
using the ordering function orderingF
.
testdata = {RandomSample[Range[6]], RandomChoice[Range[5], 3] & /@ Range[6],
RandomChoice[CharacterRange["A", "F"], 2] & /@ Range[6], RandomSample[Range[6]]};
Transpose@testdata // Grid
Take the rows in the sub-array formed by columns {2,3,4}
associated with the top 2
elements in col 1
based on default ordering Less
:
concomitantRows[Transpose@testdata, {2, 3, 4}, 1, 2]
(* Out[] = {{{3, 3, 4}, {"B", "D"}, 5}, {{4, 4, 1}, {"C", "C"}, 4}} *)
Take the ones associated with the third element:
concomitantRows[Transpose@testdata, {2, 3, 4}, 1, {3}]
(* Out[] = {{{1, 1, 5}, {"A", "B"}, 3}}*)
MemberQ[m[[1]],n]
? $\endgroup$bigger value
mean inI want to get vector in the second row that has the bigger value
? $\endgroup$