Another simple way to understand what's going on .
For me, it works well in R2014a. You only need to enter the matrix with NaN values without specifying the columns where NaN values are.
Here NaN values are replaced by 0s but you can easily modify it in the 8th line of this code:
for i= 1: s1