Message Boards Message Boards

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

GROUPS:

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:
POSTED BY: Mark Tuttle
Answer
2 years ago

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

POSTED BY: Mark Tuttle
Answer
2 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:
POSTED BY: Xavier Roy
Answer
2 years ago

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
Answer
2 years ago

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 marktuttle@pacbell.net

Thank you, -- Mark

POSTED BY: Mark Tuttle
Answer
3 months ago

Group Abstract Group Abstract