fillmissing2
Syntax
Description
specifies options using one or more name-value arguments in addition to any of the input
argument combinations in previous syntaxes. For example,
F
= fillmissing2(___,Name,Value
)fillmissing2(A,"linear",SamplePoints={1:5 10:2:18})
specifies the
locations of the sample points for the data in A
.
Examples
Replace with Nearest Nonmissing Entries
Create a 5-by-5 matrix with several NaN
values.
A = magic(5); A(1,2) = NaN; A(3:4,3:4) = NaN
A = 5×5
17 NaN 1 8 15
23 5 7 14 16
4 6 NaN NaN 22
10 12 NaN NaN 3
11 18 25 2 9
Fill missing entries of A
with nearest nonmissing entries.
F = fillmissing2(A,"nearest")
F = 5×5
17 1 1 8 15
23 5 7 14 16
4 6 7 22 22
10 12 25 3 3
11 18 25 2 9
Interpolate Missing Surface Data
Create a 51-by-51 grid, and define a function on the grid.
n = 51; [x,y] = meshgrid(linspace(-2,2,n)); f = x.^2-y.^2;
Randomly replace 5% of the values of the function with NaN
values to simulate missing data.
NaNPercent = 0.05; randEntries = randperm(n^2,round(NaNPercent*n^2)); f(randEntries) = NaN;
Fill missing entries in the data using linear interpolation. Then reshape the data for plotting.
F = fillmissing2(f,"linear");
x = reshape(x,n^2,1);
y = reshape(y,n^2,1);
f = reshape(f,n^2,1);
F = reshape(F,n^2,1);
Plot the original data together with the filled data.
filledData = scatter3(x,y,F,24,"red","filled",... MarkerEdgeColor="black"); hold on originalData = scatter3(x,y,f,24,"green","filled",... MarkerEdgeColor="black"); legend([filledData,originalData],... {"Filled","Original"},Location="north")
Use Moving Window Median
Create an 8-by-8 matrix. Randomly replace 5% of the matrix elements with NaN
values to simulate missing data.
n = 8; A = magic(n); NaNPercent = 0.05; randEntries = randperm(n^2,round(NaNPercent*n^2)); A(randEntries) = NaN
A = 8×8
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 NaN
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 NaN 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
NaN 58 59 5 4 62 63 1
Fill missing entries of A
using a 2-by-3 moving window median centered at the current position.
F = fillmissing2(A,"movmean",{2 3})
F = 8×8
64.0000 2.0000 3.0000 61.0000 60.0000 6.0000 7.0000 57.0000
9.0000 55.0000 54.0000 12.0000 13.0000 51.0000 50.0000 38.0000
17.0000 47.0000 46.0000 20.0000 21.0000 43.0000 42.0000 24.0000
40.0000 26.0000 27.0000 37.0000 36.0000 30.0000 31.0000 33.0000
32.0000 34.0000 35.0000 29.0000 28.0000 38.0000 31.4000 25.0000
41.0000 23.0000 22.0000 44.0000 45.0000 19.0000 18.0000 48.0000
49.0000 15.0000 14.0000 52.0000 53.0000 11.0000 10.0000 56.0000
40.6667 58.0000 59.0000 5.0000 4.0000 62.0000 63.0000 1.0000
Specify Missing Locations to Interpolate Outliers
Create a 5-by-5 matrix of the first 25 square roots.
A = reshape((1:25).^(1/2),[5 5])';
Replace one matrix element with a NaN
value to simulate a missing entry. Replace another matrix element with an outlying value.
A(4,4) = NaN; A(2,2) = 100
A = 5×5
1.0000 1.4142 1.7321 2.0000 2.2361
2.4495 100.0000 2.8284 3.0000 3.1623
3.3166 3.4641 3.6056 3.7417 3.8730
4.0000 4.1231 4.2426 NaN 4.4721
4.5826 4.6904 4.7958 4.8990 5.0000
Define missing entries to be those entries with either a value of NaN
or an absolute value greater than 10.
M = isnan(A) | abs(A)>10;
Fill NaN
values and outliers using the MissingLocations
name-value argument. Use the optional TF
output argument to confirm that both the NaN
entry and the outlying entry are filled.
[F,TF] = fillmissing2(A,"cubic",MissingLocations=M)
F = 5×5
1.0000 1.4142 1.7321 2.0000 2.2361
2.4495 2.6432 2.8284 3.0000 3.1623
3.3166 3.4641 3.6056 3.7417 3.8730
4.0000 4.1231 4.2426 4.3575 4.4721
4.5826 4.6904 4.7958 4.8990 5.0000
TF = 5x5 logical array
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
Use Nonuniformly Spaced Sample Points
Create a 5-by-5 matrix with several NaN
values.
A = magic(5); A(1,2) = NaN; A(3:4,3:4) = NaN
A = 5×5
17 NaN 1 8 15
23 5 7 14 16
4 6 NaN NaN 22
10 12 NaN NaN 3
11 18 25 2 9
Specify nonuniformly spaced sample points for filling missing entries using linear interpolation.
xs = [1 1.99 2 2.01 3];
ys = [1 3 8 9.5 10];
FNonuni = fillmissing2(A,"linear",SamplePoints={xs ys})
FNonuni = 5×5
17.0000 12.4286 1.0000 8.0000 15.0000
23.0000 5.0000 7.0000 14.0000 16.0000
4.0000 6.0000 7.1782 13.8812 22.0000
10.0000 12.0000 7.3564 13.7624 3.0000
11.0000 18.0000 25.0000 2.0000 9.0000
Also fill missing entries using linear interpolation with uniformly spaced sample points. Compare the results in FNonuni
and FUni
.
FUni = fillmissing2(A,"linear")
FUni = 5×5
17.0000 9.0000 1.0000 8.0000 15.0000
23.0000 5.0000 7.0000 14.0000 16.0000
4.0000 6.0000 11.3333 16.6667 22.0000
10.0000 12.0000 9.0000 6.0000 3.0000
11.0000 18.0000 25.0000 2.0000 9.0000
Input Arguments
A
— Input data
numeric matrix
Input data, specified as a numeric matrix.
Data Types: double
| single
Complex Number Support: Yes
method
— Fill method
"nearest"
| "linear"
| "natural"
| "cubic"
| "v4"
Fill method, specified as one of the values in this table.
Method | Description |
---|---|
"nearest" | Nearest nonmissing entry |
"linear" | Linear interpolation of nonmissing entries |
"natural" | Natural neighbor interpolation of nonmissing entries |
"cubic" | Cubic interpolation of nonmissing entries |
"v4" | Biharmonic spline interpolation of nonmissing entries |
movmethod
— Moving method
"movmean"
| "movmedian"
Moving method to fill missing data, specified as one of the values in this table.
Method | Description |
---|---|
"movmean" | Moving mean over a window described by
window |
"movmedian" | Moving median over a window described by
window |
window
— Window description
positive integer or duration
scalar | two-element cell array of positive integer or duration
values | two-element cell array of two-element vectors of nonnegative integer or
duration
values
Window description for moving methods, specified as a positive integer or
duration
scalar, a two-element cell array of positive integer or
duration
values, or a two-element cell array of two-element vectors
of nonnegative integer or duration
values. The window is defined
relative to the sample points.
If window
is a positive integer scalar, then the window is a
window
-by-window
block of neighboring
entries. If window
is odd, then the window is centered about the
current entry; if window
is even, then the window is centered about
the 2-by-2 submatrix of A
whose bottom-right entry is the current
entry.
If window
is a two-element cell array of positive integers
{m n}
, then the window is an
m
-by-n
block centered about the current
entry.
If window
is a two-element cell array of two-element vectors of
nonnegative integers {[bRow fRow] [bCol fCol]}
, then the window
contains the row and column of the current entry, the preceding bRow
and succeeding fRow
rows, and the preceding bCol
and succeeding fCol
columns.
If window
has type duration
, then you must
specify the SamplePoints
name-value argument as a two-element cell
array of either datetime
or duration
values.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: fillmissing2(A,"v4",SamplePoints={1:5,10:2:18})
SamplePoints
— Sample points
two-element cell array
Sample points, specified as a two-element cell array {xs ys}
of
vectors of sample point values. The sample points in the xs
vector
represent the x-axis locations of the data, and the sample points
in the ys
vector represent the y-axis locations
of the data. Both xs
and ys
must be sorted, must
not contain duplicate elements, and must match the size of A
in the
corresponding grid dimension. Sample points do not need to be uniformly sampled. If
A
is an
m
-by-n
matrix, then
1:
is the default value for
m
xs
and 1:
is the
default value for n
ys
.
Moving windows are defined relative to the sample points. For example, if
xs
and ys
are vectors of times corresponding
to the input data, then fillmissing2(rand(10,10),"movmedian",duration([0 0
5]),SamplePoints={xs ys})
uses a window that represents the interval of
2.5 seconds before and after the time of the current element. When the sample point
vectors have data type datetime
or duration
, the
moving window description must have type duration
.
Example: fillmissing2(A,"natural",SamplePoints={1:5,10:2:18})
MissingLocations
— Known missing indicator
logical matrix
Known missing indicator, specified as a logical matrix of the same size as
A
. The indicator elements can be 1
(true
) to indicate a missing value in the corresponding location
of A
or 0
(false
)
otherwise.
Output Arguments
F
— Filled data
numeric matrix
Filled data, returned as a numeric matrix. F
is the same size
as A
.
TF
— Filled data indicator
logical matrix
Filled data indicator, returned as a logical matrix. TF
is a
logical matrix where 1
(true
) corresponds to
filled entries in F
that were previously missing and
0
(false
) corresponds to unchanged
entries.
TF
is the same size as F
.
Version History
Introduced in R2023a
See Also
Functions
fillmissing
|ismissing
|standardizeMissing
|anymissing
|rmmissing
|filloutliers
|isnan
|missing
|isnat
|smoothdata
|smoothdata2
Topics
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)