Replace NaN's in table with zero
显示 更早的评论
Hello, I have a 1501x7 table called 'x' and there appears to be NaN's in the fourth and sixth column called "Age" and "height". I would like a way to replace NaN's with zeros. Take note, that I have already tried:
k = find(isnan(x))';
x(k) = 0;
% and
x(isnan(x)) = 0;
Yet, neither work because I am using a table, not a matrix. I have also tried converting my table into a cell array, and using these same functions, but they still do not work. They return:"Undefined function 'isnan' for input arguments of type 'cell'" ALSO, please note that the table has columns full of text. So, cell2mat does not work.
3 个评论
AFiorillo
2015-7-24
When indexing into a table or cell, parentheses and curly braces have very different meanings. Using parenthesis result in a slice of that table/cell, so the result is also a table or a cell. Using curly braces yields the contents of those slices. See here for a more comprehensive explanation on accessing data in tables.
For your code, try
k = find(isnan(x))';
x{k} = 0;
% and
x{isnan(x)} = 0;
xander fong
2015-7-27
hager fouda
2023-9-1
Thank you so much. it works.
采纳的回答
更多回答(9 个)
Akira Agata
2017-2-17
For example:
% Make a sample table 'T' and replace 'NaN' with 0
T = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'});
idx = ismissing(T(:,{'Age','Height'}));
T{:,{'Age','Height'}}(idx) = 0;
Yuting Mou
2016-7-29
I also run across the problem, but there seems to be an easier way:
x.age(isnan(x.age)) = 0;
This is OK in my case
1 个评论
Dooyoung Kim
2018-6-25
This works for me too! Thanks for the suggestion.
Steven Lord
2018-7-23
7 个投票
I would use the fillmissing function introduced in release R2016b. See the "Table with Multiple Data Types" example on that documentation page for a demonstration of how to replace NaN values with 0.
J.M. Verduijn
2019-2-8
for i= 1: width(T)
T.(i)(isnan(T.(i))) = 0;
end
Works for me, replaces all NaN values in table T with 0
1 个评论
Lautaro Parada
2019-7-2
This is so simple and accurate! thanks!
Zachary Smith
2020-3-19
1 个投票
If you are using readtable() to load the table from a file, then you can add the name-value pair argument 'EmptyValue',0 to do this automatically.
Aron Magesh
2021-3-7
编辑:Aron Magesh
2021-3-7
1 个投票
Just use fillmissing function if the data is in a table or timetable.
1 个评论
Walter Roberson
2021-3-7
That is what Steven Lord suggested in https://www.mathworks.com/matlabcentral/answers/231096-replace-nan-s-in-table-with-zero#answer_329967
Andy
2018-7-23
0 个投票
so, in other words:
mainTTable{:,:}(ismissing(mainTTable)) = 0;
carolina franco
2020-1-28
Hi,
Another simple way to understand what's going on .
For me, it works well in R2014a. You only need to enter the matrix with NaN values without specifying the columns where NaN values are.
%Input
m_data=C{1,1}; % Matrix with NaN values
%Code
s1=size(m_data,1);
for i= 1: s1
msubs=m_data(i,1:end); % Save existing data in ith row of m_data
msubs=msubs(isnan(m_data(i,1:end))==0); %Substitute matrix/ taking only non-NaN values
m_data(i,1:end)=0; %Erase all existing values in ith row of m_data
m_data(i,1:size(msubs,2))=msubs; %Substitute values without NaN
end
2 个评论
Stephen23
2020-1-28
Note that all the original question explicitly states that "..I am using a table, not a matrix", and all of the other answers work with tables, not numeric matrices. Tables are a container array type:
carolina franco
2020-1-28
Right! Thanks
Gabor
2021-3-11
T{:,2:4}(ismissing(T{:,2:4})) = 0;
2:4 are the columns which are containing NaN values.
1 个评论
Interesting, that does work.
T = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'})
T{:,2:4}(ismissing(T{:,2:4})) = 0
类别
在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!