replacing nan values with the mean of a moving window

조회 수: 2 (최근 30일)
Richard
Richard 2012년 11월 9일
From the following:
t = transpose(1/24:1/24:40);
data1 = 1+(30-1).*rand(length(t),1);
Randm = floor(1+(length(t)-1).*rand(37,1));
data1(Randm) = nan;
plot(t,data1);
We can see that the vector contains some missing values i.e. nan. The data shows the variation in a certain variable for 40 days i.e. there are 40 24 hours in the vector. How could I replace the missing values with the average for that particular 24 hour, in addition, if 24 consecutive values were missing i.e. no values for an entire day, how would I replace those missing values with the average for that week i.e. from the previous 7 days? I'm thinking of applying this with a moving window, would this be the best method?

채택된 답변

Image Analyst
Image Analyst 2012년 11월 9일
Here's a fairly straightforward easy to understand vectorized way of doing it:
% Reshape into 40 days (rows) by 24 columns (hours).
dailyData = reshape(data1, [40,24]);
% Get a map of where the NaNs are
nanMap = isnan(dailyData)
% Count the number of valid, non-nan values per day.
validValuesPerDay = sum(~nanMap, 2)
% Need to sum rows but can't sum with NaNs or result is a NaN,
% so replace NaNs with zeros so we can sum the row.
nonNanData = dailyData;
nonNanData(nanMap) = 0;
sumPerDay = sum(nonNanData, 2) % Sum across the row.
% Get a matrix where each row is the mean of that row.
dailyMeans = sumPerDay ./ validValuesPerDay;
% Expand it to 24 columns.
dailyMeans = repmat(dailyMeans, [1 24]);
% Replace NaNs with the mean
repairedData = dailyData; % Initialize.
repairedData(nanMap) = dailyMeans(nanMap)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by