If the matrix of component pairs would be sufficient,
then you may want to refer to this thread on the very topic:
If there is no need to fill a sparse matrix' empty cells with zeroes,
then Łukasz Majewski has a simpler variation on a theme:
Hope this could help.