Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I find what value in column 1 of my list corresponds to the minimum value in column 2?

share|improve this question
    
What value or what position? –  belisarius Sep 3 '12 at 3:14
    
Hi @belisarius, I just saw this. This is my first use of this website, and so far it is very helpful. I did need to know the value, and now I'd also like to find out how to find the position in the array. Thank you! –  Michelle Sep 3 '12 at 3:37
    
Related: (900) –  Mr.Wizard Feb 6 '13 at 1:39

7 Answers 7

Pick[] is one way to go about it:

test = {{15, 11}, {5, 14}, {2, 13}, {3, 5}, {13, 15}, {6, 10}, {8, 15}, {0, 2},
        {10, 13}, {2, 5}};

Pick[#1, #2, Min[#2]] & @@ Transpose[test]
   {0}
share|improve this answer
    
Thank you very much I used your method and it worked! –  Michelle Sep 3 '12 at 3:29
    
Can I also find which item in the array the minimum value is? How? –  Michelle Sep 3 '12 at 3:34
    
@Michelle did you read my comment right under your question? –  belisarius Sep 3 '12 at 3:35
    
Hi @belisarius, I just saw it. This is my first visit to the website and I did not at first notice that I needed to "expand" the new comments. –  Michelle Sep 3 '12 at 3:37
    
@Michelle Welcome! It takes some time to get used to the features of the site. Feel free to ask –  belisarius Sep 3 '12 at 3:39

Edit

In my earlier submission, I mistakenly took "corresponds to" as meaning "has the same value as". Here I interpret "corresponds to" as "occupies the same row as". This variation relies on Position, as before.

#1[[Position[#2, Min@#2][[1, 1]]]] & @@ Transpose@t

Exemplification

t happens to be 15x7:

t={{0, 10, 4, 19, 1, 3, 2}, {3, 18, 1, 12, 7, 14, 16}, {11, 8, 17, 18, 7, 12, 17}, {12, 16, 0, 16, 8, 3, 11}, {8, 2, 14, 3, 18, 7, 6}, {5, 15, 14, 9, 9, 3, 2}, {9, 10, 17, 6, 19, 14, 0}, {3, 5, 18, 11, 10, 12, 6}, {7, 13, 7, 13, 16, 14, 16}, {14, 12, 4, 19, 18, 20, 7}, {18,3, 19, 15, 16, 18, 8}, {1, 18, 5, 11, 3, 5, 2}, {16, 11, 7, 11, 2, 2, 19}, {1, 8, 7, 7, 15, 1, 20}, {11, 9, 2, 7, 2, 18, 4}}

The lowest value in column 2 is 2. It sits next to the value 8 in column one.

#1[[Position[#2, Min@#2][[1, 1]]]] & @@ Transpose@t

8

results

Explanation

Let's explain what the following does:

#1[[Position[#2, Min@#2][[1, 1]]]] 

First note that Min@#2returns the minimum value in the second column: 2

Now plug 2 into

Position[#2, Min@#2][[1, 1]]

obtaining

Position[#2, 2][[1, 1]]

returning `5'.

Substituting again, #1[[5]] returns the value the cell at column 1, row 5:

8

share|improve this answer

Here's a straightforward way using Cases:

list = {{1, 2}, {2, 2}, {3, 1}, {4, 1}, {2, 4}}; (* example list *)
With[{min = Min@#[[All, 2]]}, Cases[#, {x_, min} :> x]] &@list
(* {3, 4} *)

You can also use Select or Pick, depending on the usage. You should read through the documentation to see how to use them.

share|improve this answer
    
Thank you very much I used Pick[] and it worked. –  Michelle Sep 3 '12 at 3:36

Here's another way, stealing @J.M's test list

test = {{15, 11}, {5, 14}, {2, 13}, {3, 5}, {13, 15}, {6, 10}, {8, 15}, {0, 2},
        {10, 13}, {2, 5}};

First@Extract[test, test~Reverse~2~Ordering~1]

0

Remove First to see the whole row

Or probably faster is

Extract[#1, #2~Ordering~1] & @@ Transpose[test]
share|improve this answer
    
Damn, you win the cool factor. –  kale Sep 3 '12 at 3:44
    
Rojo~Reverse~Upvote –  belisarius Sep 3 '12 at 3:49
    
@belisarius, haha, I almost did it on purpose waiting for your comment –  Rojo Sep 3 '12 at 3:50
    
I knew. I tried hard not to fail you –  belisarius Sep 3 '12 at 3:50

Sure.

Not sure about "asking" but here's a way to do it.

I'll build some data for an example:

a = RandomReal[{0, 10}, {10, 2}]

And to find the value in the position in column 1 of the minimum of column 2:

a[[Flatten[Position[a[[All, 2]], Min[a[[All, 2]]]]], 1]]
share|improve this answer

Beware of the possibility of multiple minima, especially in long lists of discrete elements. I think for a novice J.M.'s solution is the more instructive. I have changed its first element to demonstrate two identical minima:

test = {{15, 2}, {5, 14}, {2, 13}, {3, 5}, {13, 15}, {6, 10}, {8, 15}, {0, 2}, {10, 13}, {2, 5}};

Pick[#1, #2, Min[#2]] & @@ Transpose[test]

out: {15, 0}

Finding the position of the minimum/minima:

min = Min@test[[All, 2]]
pos = Position[test[[All, 2]], min]

out: 2
out: {{1}, {8}}

In order to grab the corresponding values in Col. 1, the "pos" list must be Flattened first:

test[[Flatten@pos, 1]]

out: {15, 0}
share|improve this answer

With

test = {{15, 11}, {5, 14}, {2, 13}, {3, 5}, {13, 15}, {6, 10}, {8, 15},
    {0,2}, {10, 13}, {2, 5}};

variations with Ordering in Rojo's answer using Part instead of Extract:

test[[Ordering[test[[All, 2]], 1]]][[1, 1]]

or

First[#[[1]][[Ordering[#[[2]], 1]]] &[Transpose[test]]]

or

test[[Ordering[test, 1, #1[[2]] < #2[[2]] &]]][[1, 1]]
(* 0 *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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