I have a matrix (not square one) with an elements that are repeating, for example:

[![Original matrix][1]][1]

Starting from second column, I need to shift up each column in order to align in rows elements with the same values. Empty elements of a new matrix should be replaced by the zeros. In result I should have:

[![New matrix][2]][2]

Then the same pattern of a shift I should be able to apply to any other matrix with the same dimensions as the original one (but this time without such nicely repeating numbers as elements).

Could someone please help me solve this problem?

 [1]: https://i.sstatic.net/02cgS.jpg
 [2]: https://i.sstatic.net/zzanW.jpg