在Matlab中处理NaN,简单的任务 [英] Dealing with NaN in matlab, simple tasks
问题描述
我正在使用matlab中的数据.
I am working with data in matlab.
查看代码:
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)
从这开始,ww = Nan
我想如何处理这些NaN值
From this, ww = Nan
how can I deal with these NaN values, I would like ww to be,
ww = 8.5-2.5
其中8.5来自y的最后四个数字,而2.5来自z的最后四个数字.
With the 8.5 coming from the last four numbers in y and the 2.5 the last four digits in z.
我正在使用时间序列数据在MATLAB中进行回归,对于某些序列,在序列开始时缺少数据NaN.我想知道如何处理它们,上面的示例是一个Complete Minimal示例.
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.
对于一个稍微复杂的示例,尝试使用archtest
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
某些列中有NaN的地方.
Where there are NaNs in some of the columns.
推荐答案
原始问题的答案:
您可以通过提供标志omitnan
来告诉mean
函数和其他几个函数忽略NaN
值:
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')
max
,min
,sum
,prod
等其他一些功能也接受此标志.
Several other functions like max
, min
, sum
, prod
, etc. also accept this flag.
此外,如果要处理时间序列数据,则可能需要检查是否可以使用timetable
.它可以简化您的某些工作流程.
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.
已回答问题的答案:
我不确定archtest
的功能,因为这不在我的专业领域,但是如果您的工作流程只需删除NaN很好,那么您可以使用rmmissing
作为删除的预处理步骤他们.我不确定这是否会影响Lags
的含义,因为您正在删除索引.我认为在这种情况下,您将必须决定适合数据的处理方式.如果您只是想将NaN替换为其他值(例如0),则可以使用fillmissing
来实现.
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.
这篇关于在Matlab中处理NaN,简单的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!