Re: Need to speed up a matrix calculation
- To: mathgroup at smc.vnet.net
- Subject: [mg105714] Re: Need to speed up a matrix calculation
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 16 Dec 2009 06:19:22 -0500 (EST)
On 12/15/09 at 7:26 AM, warsaw95826 at mypacks.net (Garapata) wrote: >I have a calculation, which I want to do in a more direct, elegant, >and faster calculating way. >Start with 3 variables and 2 matrices: >intervals = 10; samples = 5; measurements = 3; >p = Array[a, {intervals -1, samples, measurements}]; >m = Array[b, {samples, intervals}]; >mMost = Drop[w, None, -1]; >This also shortens the length the next line, the one I need help >with. >composition = (Transpose[p[[#]]] . mMost[[All, #]] ) * 5 / >Total[mMost [[All, #]]] & /@ Range[Length[p]] >This works, but will likely get quite slow as I increase the number >of intervals, samples, and measurements. I'd like a more direct way >to do this. The above seems bit like making map like an iterative >loop, kind of cheating at functional programing. This last can be made more efficient using: In[53]:= compA = MapThread[#1.#2 &, {Transpose[p, {1, 3, 2}], Most@Transpose[m]}] 5/ Total[mMost]; In[54]:= compA == composition Out[54]= True Also, mMost is not needed in your code since you explicitly select only the first p columns of mMost which are also the first p columns of m. That is: In[56]:= compB = (Transpose[p[[#]]].m[[All, #]])*5/ Total[m[[All, #]]] & /@ Range[Length[p]]; In[57]:= compB == composition Out[57]= True