Forum Discussion

gerrardhickson's avatar
gerrardhickson
Copper Contributor
Oct 28, 2024

Summarise matrix diagonally

Hi All,

Consider a scenario where a group of people take a holiday, over the course of which, they each spend money on behalf of the group. When they get home, they need to repay others.

 

We end up with a (simplified) matrix like the one below.

 

 

Example - Person A owes Person B $85, Person B doesn't owe Person A any money.

 

Problem:

Person B and C owe each other money, and Person A and C owe each other money. How would I consolidate the amounts such that only one person from each pair transfers money to balance the bills?

 

I took the approach of multiplying all values below the diagonal line (where person A owes person A) by -1, then attempting to rotate the top diagonal half by 180deg (double transpose) to add the negative values to the bottom diagonal half.

But... this doesn't work - and if it did, it's clunky...

 

Any suggestions?

Resources