Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

Dealing with NaN in matlab, simple tasks

I am working with data in matlab.

See code:

x = rand(10,1)
y = [1,2,3,4,5,6,7,8,9,10]'
z = [NaN, NaN, NaN, NaN, 1, 2, 3, 4]'

ww = mean(z)-mean(y)

From this, ww = Nan how can I deal with these NaN values, I would like ww to be,

ww = 8.5-2.5

With the 8.5 coming from the last four numbers in y and the 2.5 the last four digits in z.

I am doing regressions in MATLAB with time series data, for certain series there is missing data NaNs at the start of the series. I am wondering how to deal with them and the above example is a Complete Minimal example.

Edit:

For a slightly more complicated example, trying to use the archtest

clear;
%data
data = xlsread('RETURNS.xlsx',2);

for jj = 2:51
    for ii = 1:12
        residuals = data(:,jj) - mean(data(:,jj));
        h(jj,ii) = archtest(residuals,'Lags',ii);
    end
end

Where there are NaNs in some of the columns.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answer for the original question:

You can tell the mean function and several others to disregard NaN values by providing the flag omitnan:

 y = [1,2,3,4,5,6,7,8,9,10]'
 z = [NaN, NaN, NaN, NaN, 1, 2, 3, 4]'

 ww = mean(z,'omitnan')-mean(y,'omitnan')

Several other functions like max, min, sum, prod, etc. also accept this flag.

And as an aside, if you are working with time series data, you may want to check out whether you can use timetable. It may simplify some of your workflows.

Answer for the updated question:

I'm not sure what archtest does, as it's not in my area of expertise, but if it's fine for your workflow to simply remove NaNs, then you can use rmmissing as a pre-processing step to remove them. I'm not sure if this will affect the meaning of the Lags, however, since you are removing indices. I think in that case you'll have to decide what's appropriate to do for your data. If you simply want to replace the NaNs with some other value (like 0), you can use fillmissing to do that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...