I was initially going to suggest
principal components analysis as a solution, since the example decomposition you presented appears to be a clustering problem. I would then have pointed you to
PDL and
PDL::PCA. However, after reading some of the other comments my poor old neuron suspects that there is something more afoot that may invalidate PCA for the general case.