Replace NaN by using for loop
3 ビュー (過去 30 日間)
古いコメントを表示
I have a set of data (call it 'dataset') which is a 1000*1000 table with NaN, NaN appear start from row 381 to the end. I design to use for loop to find the mean number from row 1 to row 380 in each column and use it to replace the NaN in that column. I have write the condition but get stuck for the loop because I have no idea code the loop.(I'm a matlab beginner)
dataset = table2array(dataset); %convert table to matrix for modify data
columns = size(dataset,2);
for i = 2:1:columns - 1 %start from second column, end at second last column
MeanNo = mean(dataset(1:380,i)); %get the mean number from row 1 to row 380 for one column
dataset(isnan(1:end,i)) = MeanNo; %replace NaN with the mean number just got
end
dataset = array2table(dataset); %convert back to table
That is my code and it cannot be run, I have no idea how can I solve it
0 件のコメント
採用された回答
Star Strider
2017 年 5 月 22 日
One approach:
dataset = [randi(9, 10, 5); NaN(10, 5)]; % Create Data
dataset_mean = mean(dataset, 'omitnan'); % Column Mean Omitting ‘NaN’ Values
dataset_new = dataset; % Create Duplicate
idx = isnan(dataset_new); % NaN == 1, Others == 0
dataset_new(idx) = 0; % Set ‘NaN’ Values = 0
dataset_new = dataset_new + bsxfun(@times, idx, dataset_mean); % Add ‘Replacement’ Values To ‘dataset_new’
4 件のコメント
その他の回答 (2 件)
Matthew Eicholtz
2017 年 5 月 22 日
When you say "it cannot be run", do you mean that you receive an error? If so, what line does the error point to and what is the error description? I'm guessing you may get an error for the following line of code:
dataset(isnan(1:end,i)) = MeanNo;
I think it should be:
dataset(isnan(dataset(1:end,i))) = MeanNo;
A couple other tips that may be helpful for the future:
1. You can replace
2:1:columns - 1
with
2:columns-1
because the default incremental value for the colon operator is 1.
2. You can compute the mean of each column in a matrix in one operation. So, if you know you will want the mean of rows 1:380 for every column, it can be computed by:
m = mean(dataset(1:380,:));
*If you wanted the mean of every row, you could use:
m = mean(dataset(1:380,:),2);
0 件のコメント
Guillaume
2017 年 5 月 22 日
fillmissing(yourtable, 'movmean', 1000)
would probably give you what you want.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!