avinterpnan(A,method)
This function enables direct and versatile replacement of nans in your data in 1D (vectors) 2D or 3D (arrays), with a large number of available methods:
'linear' - (default) linear interpolation
'nearest' - nearest neighbor interpolation
'next' - next neighbor interpolation
'previous' - previous neighbor interpolation
'spline' - piecewise cubic spline interpolation (SPLINE)
'pchip' - shape-preserving piecewise cubic interpolation
'cubic' - same as 'pchip'
'v5cubic' - the cubic interpolation from MATLAB 5, which does not
extrapolate and uses 'spline' if X is not equally
spaced.
Note ~ some of the methods above may be unavailable in earlier versions than Matlab.
To get higher dimensional data, the results from row, column and z-direction interpolations are computed separately and then simply averaged, with surprisingly robust results (even with 'linear'). The large number of methods available are because the function uses Matlab's versatile and powerful INTERP1 command.
Here are some examples:
% % 1D:
figure;
aa=[1,2,3,5,5,4,3,2,2];
dd=sqrt(aa); % make interesting data to plot
dd(4:7)=nan; % set some of it to nan's.
ee=avinterpnan(dd); % interpolate nans.
plot(dd,'r');
hold on;
plot(ee,'bo');
title('1D example:');
legend('data with nans','new data (nans interpolated with method=''linear'', the default)','Location','Best')
hold off
% % 2D:
figure;
[aa,bb]=meshgrid([1,2,3,5,5,4,3,2,2]);
dd=(aa.*bb.*(bb+aa)).*(1/2); % make an interesting looking surface
dd(5:8,4:7)=nan; % set some of it to nan's.
ee=avinterpnan(dd); % interpolate nans.
mesh(dd);title('2D example: data with nans');
xlabel('columns');ylabel('rows');figure;
surf(ee);title('2D example: new data (nans interpolated with method=''linear'', the default)')
xlabel('columns');ylabel('rows');
% % 3D:
figure;
[aa,bb,cc]=meshgrid([1,2,3,5,5,4,3,2,2]);
dd=(aa.*bb.*cc).*(1/3);dd(5:8,4:7,5:8)=nan; % make interesting 3D data
ee=avinterpnan(dd); % default method='linear' interpolation
mesh(squeeze(dd(6,:,:)));title('3D example: data with nans');
xlabel('columns');ylabel('rows');figure;
surf(squeeze(ee(6,:,:)));title('3D example: new data (nans interpolated with method)')
xlabel('columns');ylabel('rows');
% % Changing the interpolation from the default ('linear') to another method (a 2D example)
figure;
[aa,bb,cc]=meshgrid([1,2,3,5,5,4,3,2,2]);
dd=(aa.*bb.*cc).*(1/3);dd(5:8,4:7,5:8)=nan;
method='spline'; % type: help interp1 to see other available interpolation methods.
ee=avinterpnan(dd,method);
mesh(squeeze(dd(6,:,:)));title('3D example: data with nans');
xlabel('columns');ylabel('rows');figure;
surf(squeeze(ee(6,:,:)));title('3D example: new data (nans interpolated with method=''cubic'')')
xlabel('columns');ylabel('rows');
Citar como
Matt Molteno (2024). avinterpnan(A,method) (https://www.mathworks.com/matlabcentral/fileexchange/59483-avinterpnan-a-method), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Compatibilidad con las plataformas
Windows macOS LinuxCategorías
- MATLAB > Mathematics > Interpolation >
Etiquetas
Agradecimientos
Inspirado por: Inpaint over missing data in 1-D, 2-D, 3-D,... ND arrays, interp1nan, table lookup
Inspiración para: meshcompare(varargin)
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.