3
$\begingroup$

I have a list and I know the maximum value of that list by using Max[list]. The thing is I want to find out the index number of this list that corresponds to this value. For example:

list={9999,2,3,4,5,....,1}
Max[list]
(*9999*)
c= f[Max[list]]

where the function f gives me the index of list that corresponds to the value Max[list] which is 9000 in this case. Maybe using Pick somehow. Since I am dealing with frequency spectrum, and I know that the maximum is near a low frequency, I would like for the function to just check the first few or so instead of all elements in list.

$\endgroup$
2
  • 2
    $\begingroup$ Have you seen Position[]? $\endgroup$ Mar 13, 2016 at 7:35
  • $\begingroup$ @J.M. I just did! Thanks! Anyone interested: f = {Max[#], Flatten[Position[#, Max[#]]][[1]]} &; where the output is in the form (***{ max value, index in list} ***) $\endgroup$
    – phandaman
    Mar 13, 2016 at 7:43

3 Answers 3

2
$\begingroup$

try this:

First@Flatten@Position[#, Max[#]] &@list

alternative:

Last@Ordering[list]
$\endgroup$
4
  • 1
    $\begingroup$ Nice! Very similar implementation. I have been using Mathematica for well over a year now on and off and now only getting use to the Function and #& usage. I have been seeing a lot people on here using this @ I imagine its to do more than one function at a time. See my comment as well. $\endgroup$
    – phandaman
    Mar 13, 2016 at 7:45
  • 3
    $\begingroup$ The second argument of Ordering[] is quite convenient: Ordering[list, -1]. $\endgroup$ Mar 13, 2016 at 7:53
  • 1
    $\begingroup$ yes it took me some time to get a hang of the symbols too. The learning curve was steep i believe. &@ is as if you are saying apply a function on an argument, say (func &) and apply on something i.e. func&@something. And (Last@something) for instance means the same as Last[something ] $\endgroup$
    – Ali Hashmi
    Mar 13, 2016 at 7:53
  • $\begingroup$ @J.M. Spot on. I might change my code to use Ordering instead. $\endgroup$
    – phandaman
    Mar 13, 2016 at 8:17
1
$\begingroup$
list = {10, 9999, 2, 3, 4, 5, 1};

Using PositionLargest (new in 13.2)

PositionLargest[list]

{2}

Positions of the 2 largest values

PositionLargest[list, 2]

{{2}, {1}}

With TakeLargest (new in 10.1) we can get positions and elements in one go:

TakeLargest[list -> {"Index", "Element"}, 2]

{{2, 9999}, {1, 10}}

$\endgroup$
1
$\begingroup$

Grabbing the @eldo's list and using MaximalBy:

list = {10, 9999, 2, 3, 4, 5, 1};

List @@@ MaximalBy[Rule @@@ Thread@{Range[Length@#], #} &@list, Last]

(*{{2, 9999}}*)

As with TakeLargest, with MaximalBy we can obtain more positions and elements:

List @@@ MaximalBy[Rule @@@ Thread@{Range[Length@#], #} &@list, Last, 2]

(*{{2, 9999}, {1, 10}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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