1
$\begingroup$

I have plotted a graph from a CSV file which consists of two columns of data Time and Intensity. I simply want to find the time where the intensity is largest.

This code:

MaxIntensity = Max[Data[[All, 2]]]

outputs the correct value for the peak y axis value.

However, this code:

Position[Data[[All, 1]], MaxIntensity]

just outputs {}.

Also, this code:

Position[Data, MaxIntensity]

outputs {{429, 2}}.

Looking at the CSV file in a text editor the 429 is the line number where the peak value occurs, but I can't figure out what that 2 means and I can't use that output to pick out the corresponding time for index 429.

Any ideas?

$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Mar 18 '15 at 22:23
  • $\begingroup$ The "2" means it comes from the second column, i.e. you're finding the position of the intensity in a 2D list. $\endgroup$ – dr.blochwave Mar 18 '15 at 22:29
  • $\begingroup$ How can I extract the Row number only then so I can use that further? $\endgroup$ – Q.P. Mar 18 '15 at 22:31
1
$\begingroup$

The simplest modification to your already existing approach is this:

data = {{0, 98}, {1, 100}, {2, 110}, {3, 99}, {4, 100}}

(* ==> {{0, 98}, {1, 100}, {2, 110}, {3, 99}, {4, 100}} *)

maxIntensity = Max[data[[All, 2]]]

(* ==> 110 *)

First@Flatten[Position[data, {_, maxIntensity}]]

(* ==> 3 *)

Here, I used Position on data directly, but with a pattern in which the first entry of the pair {_, maxIntensity} means that it is irrelevant for the position search. Have a look at Blank. The First@Flatten is needed because Position could return more than one match, and I assume you only want the first match. If you want all of them, just omit First.

There are also other approaches. For example, you could use Ordering:

Ordering[data, 1, #1[[2]] > #2[[2]] &]

(* ==> {3} *)
$\endgroup$
1
  • $\begingroup$ Beautiful. Thanks! $\endgroup$ – Q.P. Mar 18 '15 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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