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');
Zitieren als
Matt Molteno (2024). avinterpnan(A,method) (https://www.mathworks.com/matlabcentral/fileexchange/59483-avinterpnan-a-method), MATLAB Central File Exchange. Abgerufen .
Kompatibilität der MATLAB-Version
Plattform-Kompatibilität
Windows macOS LinuxKategorien
- MATLAB > Mathematics > Interpolation >
Tags
Quellenangaben
Inspiriert von: Inpaint over missing data in 1-D, 2-D, 3-D,... ND arrays, interp1nan, table lookup
Inspiriert: meshcompare(varargin)
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
Version | Veröffentlicht | Versionshinweise | |
---|---|---|---|
1.1.0.0 | Changed the text in the description (made the examples better). |
||
1.0.0.0 |