问题
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