Replace NaN's in table with 0

≡放荡痞女 提交于 2019-12-13 02:58:57

问题


I have a table in matlab R2015b with the following data

                var 1         var 2        var 3       
Row1         1             NaN           2
Row2         2                4            NaN

I'd like to replace all NaN's in my table with 0's - so something along these lines:

Assume my table is called A

newTableA = rowfun(@(x) x(isnan(x)) = 0,A,'ExtractCellData',true);  

I suppose I could convert my table to a new matrix B, perform, B(isnan(B)) = 0, and convert back to a table, but I thought there might be a more efficient way.


回答1:


Just loop over the variables:

t = array2table([1 nan 2; 2 4 nan])
for i=1:size(t,2)
    x = t{:,i};
    x(isnan(x)) = 0;
    t{:,i} = x;
end

If you checkout the source code of methods like:

>> which table\ismissing
>> which table\standardizeMissing

you'll see that is more or less what they do anyway...



来源:https://stackoverflow.com/questions/36315985/replace-nans-in-table-with-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!