# Nested calls to Total to sum over middle dimension of 2X2X2 array?

Posted 3 years ago
4470 Views
|
4 Replies
|
1 Total Likes
|
 In an inner-inner "loop" I want to compute the binary marginal sums of a 2X2X2 array. The 1st and 3rd dimensions require only Total[data,{-2,-1}] and Total[data,{1,2}] respectively. The 2nd - middle dimension - seems to require a nested call - Total[Total[data,{1}],{2}].Am I missing something in my use of Total?Don't want to use Transpose here. Eventually, I'd like to generalize this to higher dimensions.Example attached.Thank you, --Mark Attachments:
4 Replies
Sort By:
Posted 3 years ago
 Title should read "Nested calls to Total to sum over middle dimension of 2X2X2 array?" ...
Posted 3 years ago
 Hi Mark,I did not succeed to obtain the sum over the second dimension using Total in the way you showed. An alternative approach which doesn't make use of Transpose would be the following Total[a2X2X2[[#, All, All]], 2] & /@ {1, 2} Total[a2X2X2[[All, #, All]], 2] & /@ {1, 2} Total[a2X2X2[[All, All, #]], 2] & /@ {1, 2} It can be easily generalized to tensors of any rank and/or dimensions. For high ranks and small number of dimensions, the timings between this version and yours are more or less similar; but for small ranks and high number of dimensions, the above version is significantly slower. (See attached file for these remarks.) Attachments: