Let's suppose that I have table t = {1.23, 3.4, 10, 34, 1.78, 0.98, 12.56}.
To take the biggest element I can use Max[t] function. But I want to take not only the maximum element, but also the index of the element (so, which element in table it is).
Is any function which helps to take the indes of element, when I know the value of this element?
Asked
Active
Viewed 3.0k times
19
Sjoerd C. de Vries
- 65,815
- 14
- 188
- 323
Ziva
- 817
- 1
- 6
- 13
1 Answers
53
Ordering[t, -1]
which is orders of magnitude faster than
Position[t,Max[t]]
Sjoerd C. de Vries
- 65,815
- 14
- 188
- 323
-
Since the op states after initial example " Is any function which helps to take the indes of element, when I know the value of this element?", a much more general statement, not sure ordering will address it. Of course, if one is only after the last instance of a maximum (because if there are multiple, ordering again becomes a bit useless), this is the fastest way. – ciao Jan 22 '14 at 23:21
-
2Make sure to actually get the max index out of the list that "Ordering" returns.
Ordering[t,-1][[1]]– sffc Oct 14 '15 at 21:27 -
Position[t,Max[t]]– ciao Jan 22 '14 at 22:24Position. If you have a more specific question please edit accordingly. – Mr.Wizard Jan 22 '14 at 23:32Positionby searching for "index" the second result of which is the page forStringPosition, and the first link under "See Also" on that page is toPosition. Convoluted, I know, but that's fairly common, so if you want to learn to use the documentation I suggest getting used to it. Another search term that would have been more direct is "Find" the second result for which isPosition. – Mr.Wizard Jan 23 '14 at 03:02Orderingplease read: (2729) – Mr.Wizard Jan 23 '14 at 03:03