Principal-component analysis of two-particle azimuthal correlations in PbPb and pPb collisions at CMS