Re: Array reference help please

*To*: mathgroup at smc.vnet.net*Subject*: [mg73316] Re: Array reference help please*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Sun, 11 Feb 2007 01:18:15 -0500 (EST)*Organization*: The Open University, Milton Keynes, UK*References*: <eqjiid$7te$1@smc.vnet.net>

rob wrote: > I've managed to get this code working but I'm stumped on a > good way to refer to the entries in the data when plotting. > The only thing I can get to work is to refer to the first of > each pair as Part[data[[i]],1] and the second as > Part[data[[i]],2]. > > And more humiliating, the second one I have to spell out > twice. I tried all kinds of substitutions but failed. I know > you gurus here know how to make this code look better and > run more quickly. Thanks for your time. > > > > Î±=100;(* decay parameter*) > > (* first parameter is pulse amplitude, second is time of > occurrence - later to follow Poisson *) > > data=Table[Random[],{40},{2}]; > > li=Length[data]; > > Plot[Sum[ > Part[data[[i]],1] UnitStep[t-Part[data[[i]],2]] > Exp[-Î± (t-Part[data[[i]],2])], {i,li} > ] > ,{t,0,1}] > Hi Rob, Here is one possible answer: In[1]:= Î± = 100; data = Table[Random[], {1000}, {2}]; li = Length[data]; sum1 = Sum[data[[i]][[1]]*UnitStep[t - data[[i]][[2]]]* Exp[(-Î±)*(t - data[[i]][[2]])], {i, li}]; sum2 = Plus @@ (First[#1]*UnitStep[t - Last[#1]]* Exp[(-Î±)*(t - Last[#1])] & ) /@ data; sum2 == sum1 Timing[Plot[Sum[data[[i]][[1]]*UnitStep[ t - data[[i]][[2]]]* Exp[(-Î±)*(t - data[[i]][[2]])], {i, li}], {t, 0, 1}]; ] Timing[f = Plus @@ (First[#1]*UnitStep[t - Last[#1]]* Exp[(-Î±)*(t - Last[#1])] & ) /@ data; Plot[f /. t -> t, {t, 0, 1}]; ] Out[6]= True Out[7]= {16.094 Second,Null} Out[8]= {6.062 Second,Null} Regards, Jean-Marc