FULL CREDIT FOR THE DEVELOPMENT OF THIS CODE TO BENJAMIN KRAUS (2013). I expanded his Matlab Exchange entry (https://www.mathworks.com/matlabcentral/fileexchange/41961-nanconv) to allow for N dimensions.
Below follows Benjamin Kraus' explanation of how this code works. I decided to transcribe it here, since he did such a great job!
NANCONVN runs CONVN either two or three times. The first time is run on the original input signals A and K, except all the NaN values in A are replaced with zeros. The 'same' input argument is used so the output is the same size as A. The second convolution is done between a matrix the same size as A, except with zeros wherever there is a NaN value in A, and ones everywhere else. The output from the first convolution is normalized by the output from the second convolution. This corrects for missing (NaN) values in A, but it has the side effect of correcting for edge effects due to the assumption of zero padding during convolution. When the optional 'noedge' parameter is included, the convolution is run a third time, this time on a matrix of all ones the same size as A. The output from this third convolution is used to restore the edge effects. The 'noedge' parameter is enabled by default so that the output from 'nanconv' is identical to the output from 'conv2' when the input argument A has no NaN values.
Fernando Zigunov (2020). nanconvn (https://www.mathworks.com/matlabcentral/fileexchange/74721-nanconvn), MATLAB Central File Exchange. Retrieved .