Often, you represent missing or
unavailable data values in MATLAB® code with the
which stands for Not-a-Number.
The IEEE® floating-point arithmetic convention defines
the result of an undefined operation, such as 0/0.
When you perform calculations on a IEEE variable that contains
NaN values are propagated to the final result.
This behavior might
render the result useless.
For example, consider a matrix containing
the 3-by-3 magic square with its center element replaced with
a = magic(3); a(2,2) = NaN a = 8 1 6 3 NaN 7 4 9 2
Compute the sum for each column in the matrix:
sum(a) ans = 15 NaN 15
Notice that the sum of the elements in the middle column is
NaN value because that column contains a
If you do not want to have
NaNs in your final
these values from your data. For more information, see Removing NaNs from Data.
Use the IEEE function
isnan to identify
in the data, and then remove them using the techniques in the following
Use the function
Find the indices of elements in a vector
Remove any rows containing
If you remove
NaNs frequently, consider creating a small function that you can call. For example:
function X = exciseRows(X) X(any(isnan(X),2),:) = ;
After you remove all rows containing
use the following command to compute the correlation coefficients
C = corrcoef(exciseRows(X));
For more information about correlation coefficients, see Linear Correlation.
Use interpolation to
find intermediate points in your data. The simplest function for performing
is a 1-D interpolation function.
By default, the interpolation method is
which fits a straight line between a pair of existing data points
to calculate the intermediate value. The complete set of available
methods, which you can specify as arguments in the
includes the following:
'nearest' — Nearest neighbor
'next' — Next neighbor interpolation
'previous' — Previous neighbor
'linear' — Linear interpolation
'spline' — Piecewise cubic
Shape-preserving piecewise cubic interpolation
'v5cubic' — Cubic interpolation
from MATLAB Version 5. This method does not extrapolate, and
it issues a warning and uses
not equally spaced.
For more information, see
interp1 or type
help interp1 at
the MATLAB prompt.