I don't see any D^´2 in your file. Furthermore we would need your data file to see where the problem occurs.
BTW it's rather confusing and unclear to spread the calculations over six pages width! At least I won't look at that kind of sheet.
Nevertheless I can image why your routine fails if the argument is just a one column vector: cols(M) returns 1. Your for-loop therefore runs down from 1 to zero and at first you try to access M_column<1> which does not exist --> error
Changing the rouitine to the following should solve the problem (additionally the first assignement assures that filterNaN() will not fail because of the nonexistence of a NaN):