isnan to conver nan to zero in a table
8 次查看(过去 30 天)
显示 更早的评论
Hi All,
Apologies for what might be a silly question but I am stuck at the moment.
I have used readtable to input a text formatted CSV file.
T=readtable(zz,'Delimiter', '\t');
This gives a 39x501 table.
The table has NaN values which I need to replace with zeros. Based on anoher answer, I am trying:
T(isnan(T)) = 0;
but get the error
Incorrect number or types of inputs or outputs for function isnan.
Help :-)
Darren
0 个评论
采纳的回答
Steven Lord
2025-2-10
Let's make a sample table with some NaN values and a non-numeric variable.
A = magic(4);
A(randperm(numel(A), 3)) = NaN;
T = array2table(A);
T.A5 = ["apple"; "banana"; "cherry"; "durian"]
You can use the fillmissing function to replace the NaN values in the numeric variables with the constant 0.
T2 = fillmissing(T, 'constant', 0, 'DataVariables', @isnumeric)
If all the variables are numeric you can omit the 'DataVariables' name-value argument.
更多回答(1 个)
Fangjun Jiang
2025-2-10
T is an object of the class "table". You can't apply isnan() on it directly. Try the 'TreatAsMissing' option of the readtable() function to avoid NaN first hand.
1 个评论
Walter Roberson
2025-2-10
TreatAsMissing gives additional data patterns that are to have NaN (or appropriate missing indicator) substituted. TreatAsMissing does not give any opportunity to substitute a constant value for missing data.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!