fast summing alternative?
- To: mathgroup at smc.vnet.net
- Subject: [mg128332] fast summing alternative?
- From: Chris <kristoph.steikert at gmail.com>
- Date: Mon, 8 Oct 2012 02:28:43 -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
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