# Test First row of matrix

What is a good way to test the first row of a Matrix to check if one of the values equal a value. For instance, In this example, I want to get vector in the second row that has the bigger value in the first row, 10.

I can quickly do it with a lop, but don't think it's the best way...

-
Perhaps MemberQ[m[[1]],n] ? –  David Carraher Oct 6 '12 at 20:56
What does bigger value mean in I want to get vector in the second row that has the bigger value ? –  David Carraher Oct 6 '12 at 20:59
I meant that in the example, I want to know that the bigger value is 10, and therefor the vector I'm interested in is {1,1,1}. In another example of a 3x3, the values at the first row may be 1, 4, 3 and I need to detect that the bigger value is now in position 2 (number 4), and will use that to get the vector in the second row, below the number 4. –  whynot Oct 6 '12 at 21:28

Data

m = {{10, -2, 1}, {{1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10}}}


Code

This gives the vector in row 2 that is opposite (i.e. under) the largest value (or first of multiple largest values) in row 1:

m[[2, Position[m[[1]], Max[m[[1]]]][[1, 1]]]]


{1, 2, 3}

Analysis

Max[m[[1]]] gives the largest value in row 1: 10

Position[m[[1]], Max[m[[1]]]][[1, 1]] gives the column of interest: 1

-

So for your general question you will want to use Position/Extract, let say for the example you give to get the vector that corresponds to 1 (which is neither the largest, nor the smallest ...) you could do:

Extract[mat[[2, All]], Position[mat[[1, All]], 1]]


Now if you want to generally get results that deal with numeric ordering, you can use b.gatessucks answer, or the one I prefer is:

mat[[2, Ordering[mat[[1, All]], -1]]]


Where -1 gives the largest value, 1 gives the smallest, and any other integer gives the relative largest/smallest (if negative)

-

If you define your input as

matrix = {{10, -2, 1}, {a, b, c}};


then you can do

Last@Last@SortBy[Transpose[matrix], #[[1]] &]

-
A variant is to use Ordering: Last@(Transpose@mat)[[Last@Ordering@First@mat]] –  murray Oct 6 '12 at 21:34

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}}*)

-

One can use Pick[] for the purpose:

m = {{10, -2, 1}, {{1, 1, 1}, {-3, 1, 3}, {1, -2, 1}}};

First@Pick[Last[#], First[#], Max[First[#]]] &@m
{1, 1, 1}

-