Re: fast summing alternative?
- To: mathgroup at smc.vnet.net
- Subject: [mg128369] Re: fast summing alternative?
- From: Ray Koopman <koopman at sfu.ca>
- Date: Wed, 10 Oct 2012 01:26:13 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <k4tth1$sgq$1@smc.vnet.net> <k50a7p$3sh$1@smc.vnet.net>
On Oct 8, 9:48 pm, Ray Koopman <koop... at sfu.ca> wrote: > On Oct 7, 11:59 pm, Chris <kristoph.steik... at gmail.com> wrote: >> Dear all >> >> The code below is part of a large compile function I wrote and the inputs, like the matrices weights and newData, are in fact large data files. Thus, I provide them in the following problem as random. Although, written as compile code (with CompilationTarget->"C") it is still too slow for the amounts of data I have to process. I provide two of my fastest alternatives, which sadly are not that fast. I do appreciate any help to make the code faster. Thanks in advance! >> >> (* >> What I do is simple: For each row of newMat and weights I construct a new row. The elements of this new row are the sums of the elements in weights if a certain requirement of the same element in newMat is met. >> *) >> >> Clear[bNum,nNum,weights,newMat,sortMat,tab]; >> >> bNum; >> nNum=100; >> >> weights=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}]; >> newMat=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}]; >> sortMat=Table[i,{i, -10, 10, 0.1}]; (*elements always evenly spaced*) >> >> (*first alternative*) >> tab=Table[ >> Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}], {k,1,bNum}, >> {j,1,Length[sortMat]}];//AbsoluteTiming >> >> (*second alternative*) >> mat=Block[{k=1}, >> Reap[Do[ >> Sow[Table[Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{j,1,Length[sortMat]}]]; >> k++,{bNum}]]][[2,1]];//AbsoluteTiming > > tab2 = Transpose @ With[{negnewMat = - newMat}, MapThread[ > Dot, {weights, UnitStep[# + negnewMat]} ]& /@ sortMat ] This should be faster: tab3 = Transpose[ MapThread[Dot,{weights,#}]& /@ Partition[ UnitStep[Plus@@Transpose@Tuples@{sortMat,-newMat}], bNum] ]