Dealing with Nan's in Matlab Functions

Dealing with NaN's in matlab functions

You could do something like mean(x(~isnan(x))). If you want you could also write a bunch of wrappers like this and put them in your startup.m file.

how to deal with nans when using an exponential smoothing function

If there are nans in your data, you should definitely ignore them when doing such processing (should those points be 0? -inf? inf? 10.5? pi?). The main point is, as @IKavanagh pointed out, that you have to actually ignore those points entirely, i.e. drop the v elements as well that correspond to the nans:

v = datenum(2008, 1, 1):datenum(2010, 11, 31); % time vector
d = rand(1,length(v)); % data vector

d(10) = nan;

v = v(~isnan(d));
d = d(~isnan(d));

%do the rest of the processing as usual

fd = d;
alpha_o = 0.2;
for ii = 2:length(fd);
fd(ii) = alpha_o.*fd(ii) + (1-alpha_o).*fd(ii-1);
end

plot(v,d); hold on; plot(v,fd);

The fact that nans are present in your data show you that those data points are invalid, so you have to get rid of them for smoothing/fitting (and plot will automatically ignore those).

MATLAB time-series regression, dealing with NaNs

Just select the not NaNs

%Data
data = rand(1000,10); %add NaNs randomly at the start of the series.

%Regression
for ii = 2:10
notnans=~isnan(data(:,ii)); % assuming data(:,1) has no NaNs
b = regress(data(notnans,1),data(notnans,ii))
end

Matlab disregarding NaN's in matrix

Have a look at nanstd (stat toolbox).

The idea is to center the data using nanmean, then to replace NaN with zero, and finally to compute the standard deviation.

See nanmean below.

  % maximum admissible fraction of missing values
max_miss = 0.6;

[m,n] = size(x);

% replace NaNs with zeros.
inan = find(isnan(x));
x(inan) = zeros(size(inan));

% determine number of available observations on each variable
[i,j] = ind2sub([m,n], inan); % subscripts of missing entries
nans = sparse(i,j,1,m,n); % indicator matrix for missing values
nobs = m - sum(nans);

% set nobs to NaN when there are too few entries to form robust average
minobs = m * (1 - max_miss);
k = find(nobs < minobs);
nobs(k) = NaN;

mx = sum(x) ./ nobs;

See nanstd below.

  flag = 1; % default: normalize by nobs-1

% center data
xc = x - repmat(mx, m, 1);

% replace NaNs with zeros in centered data matrix
xc(inan) = zeros(size(inan));

% standard deviation
sx = sqrt(sum(conj(xc).*xc) ./ (nobs-flag));

Taking the mean of a matrix with NaN's in Matlab

You can use isnan() to filter out the unwanted elements:

mean(A(~isnan(A)))

Best way of handling NaN in MATLAB/MEX

Here is an example to create a numeric vector filled with NaNs from a MEX-function:

test_nan.cpp

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
plhs[0] = mxCreateDoubleMatrix(3, 1, mxREAL);
double *x = mxGetPr(plhs[0]);
double nanVal = mxGetNaN();
for (int i=0; i<3; ++i) {
x[i] = nanVal;
}
}

MATLAB

>> mex -largeArrayDims test_nan.cpp
>> x = test_nan()
x =
NaN
NaN
NaN


Related Topics



Leave a reply



Submit