Missing values - Running average

1 回表示 (過去 30 日間)
Tushar Agarwal
Tushar Agarwal 2016 年 12 月 28 日
コメント済み: Tushar Agarwal 2016 年 12 月 28 日
First of, I hvae this huge dataset, with 84000 rows and 24 cols. However, there are missing data cells within this excel file. Most of these cells have a value above and below them. Now I wanted to fill this gap, with the running average from above and below this cell.
How can I do this, in a less time-consuming way - New to Matlab.

採用された回答

Image Analyst
Image Analyst 2016 年 12 月 28 日
That's not huge - it's only about a tenth the size of a typical digital image. Anyway, you will get nan in the data where the Excel cell is empty. So use conv2() and isnan() to replace the missing ones. Assuming there is only one missing cell, not a string of them, do something like this:
%data = rand(18, 24); % Create sample/test data.
%data(5, 2) = nan;
data = xlsread(filename);
% Find missing cells.
nanLocations = isnan(data);
% Assign zeros to nan locations
data(nanLocations) = 0;
% Count non-nans
kernel = [1;1;1];
nonNanCounts = conv2(double(~nanLocations), kernel, 'same');
% Sum up values in 3x1 window.
sums = conv2(data, kernel, 'same');
% Divide the sum by the count to get the mean.
meanMatrix = sums ./ nonNanCounts;
% Replace nan's in original data with mean values.
data(nanLocations) = meanMatrix(nanLocations);
  1 件のコメント
Tushar Agarwal
Tushar Agarwal 2016 年 12 月 28 日
Thanks alot, that worked! :)

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 12 月 28 日
Another way is to use the regionfill() function.

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by