Hello Andrea,
I understand that you are trying to replace the missing values (nan) with values based on the neighbouring elements.
One way of doing this is by using “fillmissing2” function of MATLAB. This function operates on 2-Dimensional data that has missing values. The type of interpolation to be done can be passed in as an input to the functional call. In your case once the 3-Dimensional S matrix data is generated with some nan values, data along each dimension x, y and z can be treated as an individual 2-Dimensional matrix and can be passed on to the function. Then the missing values are fixed and a better surf plot is obtained. Here is an example code which uses “fillmissing2”:
S=peaks(25);%example data
surf(S);%original surf plot
idx1=randi(25,3);
idx2=randi(25,3);
S(idx1,idx2)=nan;%removing data from random places of the data
surf(S);%plotting with missing data
S=fillmissing2(S,"nearest");
surf(S);%plot after replacing nan values
There are several other functions like “fillgaps”, “interp2”, etc that can be used for achieving similar results.
You can refer to the below documentations to learn more about such functions.
- “fillmissing2”: https://www.mathworks.com/help/matlab/ref/fillmissing2.html
- “interp2”: https://www.mathworks.com/help/matlab/ref/interp2.html
- “fillgaps”: https://www.mathworks.com/help/signal/ref/fillgaps.html
Hope this helps.
Thanks,
Charan.