Re: message 50001?
- To: mathgroup at smc.vnet.net
- Subject: [mg50042] Re: [mg50001] message 50001?
- From: Tomas Garza <tgarza01 at prodigy.net.mx>
- Date: Thu, 12 Aug 2004 05:44:31 -0400 (EDT)
- References: <200408110952.FAA04080@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
As you know, For loops are a no-no in Mathematica. I suggest the following - and I'm aware there will be dozens of more elegant and straightforward solutions in this MathGroup. First construct an example. I use an array of random numbers drawn from a binomial distribution, and then take their frequencies. This is a list of {x, y} pairs with a peak somewhere. I assume you have loaded the Statistics package beforehand. In[1]:= dist = BinomialDistribution[10, 0.5]; In[2]:= aList = RandomArray[dist, 100]; In[3]:= s = Reverse /@ Frequencies[aList] Out[3]= {{2, 3}, {3, 14}, {4, 20}, {5, 22}, {6, 20}, {7, 13}, {8, 7}, {9, 1}} The peak in this example is 22 and corresponds to the index 5. Locate the maximum within the y-values, determine its position in the list, and use this position to find the corresponding x-value: In[4]:= t = Transpose[s] Out[4]= {{2, 3, 4, 5, 6, 7, 8, 9}, {3, 14, 20, 22, 20, 13, 7, 1}} In[5]:= t[[1,Position[t[[2]], x_ /; x == Max[t[[2]]]][[1,1]]]] Out[5]= 5 This may be extended to more general situations where, for example, the peak is attained at several x-values. Tomas Garza Mexico City ----- Original Message ----- From: "1.156" <rob at pio-vere.com> To: mathgroup at smc.vnet.net Subject: [mg50042] [mg50001] message 50001? > Mathematica wizards, I've managed to cripple together code to do something that > I feel must be doable more easily and/or faster. > > S is a list of reals containing a signal with a prominent peak in > amplitude. I simply want to find the index of the peak (x, the number of > entries into the list where the peak occurs). Here's what I did. > > > For[i = 1, S[[i]] ? max[S], i++, x = i]; > > Can some suggest a better way, especially one not using a For loop? > > Thanks, Rob > >
- References:
- message 50001?
- From: "1.156" <rob@pio-vere.com>
- message 50001?