Message Boards Message Boards

4 Replies
1 Total Likes
View groups...
Share this post:

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

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

POSTED BY: Mark Tuttle
4 Replies

Title should read "Nested calls to Total to sum over middle dimension of 2X2X2 array?" ...

POSTED BY: Mark Tuttle
Posted 9 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.)

POSTED BY: Xavier Roy

Wow! This is why I love this community. Your solution is very helpful; I will be able to adapt it for a number of related things! Thank you very much. -- Mark

POSTED BY: Mark Tuttle

Dear Xavier,

I've been able to both generalize (as you proposed) and leverage your suggestion re tensor dimensional sums.

I'd like to give you credit, but there are too many Xavier Roys on the Web.

If you feel like please send contact info to me at my regular email

Thank you, -- Mark

POSTED BY: Mark Tuttle
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract