replacing every columns of nan with the mean of each columns

1 回表示 (過去 30 日間)
crixus
crixus 2015 年 3 月 19 日
編集済み: crixus 2015 年 3 月 19 日
Hi all, i want to replace every columns of nan with the mean of each columns and i used the following code
median1 = nanmean(combinedatamat)
[rows, columns] = size(combinedatamat);
n = 0 ;
for col = 1 : columns
n = n + 1;
combinedatamat(isnan(combinedatamat(:,n))) = median1(1,n) ;
end
What i get is the first column of combinedatamat being replaced with the last columns of median1 while the rest of the columns in combinedatamat still contain NAN, can i know what is the error here ? thanks in advance
  1 件のコメント
crixus
crixus 2015 年 3 月 19 日
編集済み: Guillaume 2015 年 3 月 19 日
median = nanmean(combinedatamat);
for n = 1 : size(combinedatamat,2)
for nn = 1:size(combinedatamat,1)
if (isnan(combinedatamat(nn,n)))
combinedatamat(nn,n) = median(:,n);
end
end
end
this seems to work well.

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

採用された回答

Guillaume
Guillaume 2015 年 3 月 19 日
A simple vectorised way of achieving what you want:
median1 = repmat(nanmean(combinedatamat), size(combinedatamat, 1), 1);
combinedatamat(isnan(combinedatamat)) = median1(isnan(combinedatamat))
  1 件のコメント
crixus
crixus 2015 年 3 月 19 日
編集済み: crixus 2015 年 3 月 19 日
Wow ! nice ! Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by