Re: "stepwise" integrating a 2D array
- To: mathgroup at smc.vnet.net
- Subject: [mg91262] Re: "stepwise" integrating a 2D array
- From: Claus <claus.haslauer at web.de>
- Date: Tue, 12 Aug 2008 04:46:36 -0400 (EDT)
- References: <g7h9mc$b73$1@smc.vnet.net> <g7k0am$i86$1@smc.vnet.net>
Jens-Peer, beautiful! Thanks Jens-Peer! > Hi, > > try > > hvAccumulate[m_] := > Reverse[Rest[FoldList[#1 + #2 & , 0, #]] & /@ > Transpose[ > Rest[FoldList[#1 + #2 & , 0, #]] & /@ Transpose[Reverse[m]]]] > > and > > hvAccumulate[{{a1, a2}, {b1, b2}}] > > gives > > {{a1 + b1, a1 + a2 + b1 + b2}, > {b1, b1 + b2}} > > Regards > Jens > > Claus wrote: >> Hi, >> I have an array, consisting of n tuples (x,y,z). >> The first entry is the point at the bottom left (thinking in a >> coordinate system, smallest (x,y) pair). >> Now I want to do something like an "stepwise"/empirical integral over (x,y). >> >> Schematically, if I had a 2x2 array (only the z-values),like >> >> a1 a2 >> b1 b2 >> >> I would want an array like this: >> >> a1+b1 b1+b2+a1+a2 >> b1 b1+b2 >> >> In the code below, "xyzVals" is the array that I want to integrate, >> containing the (x,y,z) tuples. >> >> I could do a bunch of for loops, but I am trying to not use do loops in >> mathematica whenever possible. >> Is it possible to do it without? >> I tried some of the examples from >> http://www.verbeia.com/mathematica/tips/HTMLLinks/Tricks_A-K_3.html >> and I also thought about an array with sub-arrays, >> but couldn't get it for me to work. >> >> Thanks for any pointers! >> Claus >> >> >> In[13]:= X = RandomReal[{0, 1}, 500]; >> Y = RandomReal[{0, 1}, 500]; >> BinWidth = 0.2; >> DensVals = >> Flatten[BinCounts[ >> Transpose[{X, Y}], {0, 1, BinWidth}, {0, 1, BinWidth}]] >> >> Out[16]= {16, 16, 17, 26, 23, 23, 16, 18, 25, 17, 13, 26, 26, 21, 19, \ >> 15, 32, 20, 21, 15, 11, 26, 16, 19, 23} >> >> In[17]:= xyzVals = Table[{i/(1/BinWidth) + (BinWidth/2), >> j/(1/BinWidth) + (BinWidth/2), >> DensVals[[(1/BinWidth) i + j + 1]]} >> , {i, 0, (1/BinWidth) - 1} >> , {j, 0, (1/BinWidth) - 1}] >> >> Out[17]= {{{0.1, 0.1, 16}, {0.1, 0.3, 16}, {0.1, 0.5, 17}, {0.1, 0.7, >> 26}, {0.1, 0.9, 23}}, {{0.3, 0.1, 23}, {0.3, 0.3, 16}, {0.3, 0.5, >> 18}, {0.3, 0.7, 25}, {0.3, 0.9, 17}}, {{0.5, 0.1, 13}, {0.5, 0.3, >> 26}, {0.5, 0.5, 26}, {0.5, 0.7, 21}, {0.5, 0.9, 19}}, {{0.7, 0.1, >> 15}, {0.7, 0.3, 32}, {0.7, 0.5, 20}, {0.7, 0.7, 21}, {0.7, 0.9, >> 15}}, {{0.9, 0.1, 11}, {0.9, 0.3, 26}, {0.9, 0.5, 16}, {0.9, 0.7, >> 19}, {0.9, 0.9, 23}}} >> >