No. You cannot "fix" it, in the sense that you will always get something with a NaN, using the code you have on the data you have. YOU might be able to fix the problem, in the sense that you can change your code to avoid the problem. For example, are you randomly sometimes selecting the same point twice in your selection?
NaNs result from several operations. For example, what is the result of inf-inf? Is it zero? Is it inf?
Likewise, what is 0/0? Again, it can arguably be ANY number, anything from 0 to 1 to 42 to inf, depending on how you take a limit.
How about inf/inf? Is it 1? 0? inf?
inf - inf
So your data has something in it that probably creates some infs (and therefore) some NaNs in it. Or it has a 0/0 operation in it. I cannot possibly know what did it, because I don't have your data.
Learn to use the debugger. It can help to detect where the problem arises.
dbstop if naninf