I have a table with many variables and some of the entries have NaN
´s. I would like to replace all NaN
with 0. I am able to find the NaN
´s with this:
T = table(rand(200,1),[rand(199,1);NaN],rand(200,1));
Index = varfun(@isnan,T);
However, when I try to apply the index to the table, T:
T(Index)=0;
I get the following error:
You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.
Is there an easy solution to this, or do I need to change the from table to a matrix?
The issues seems to be that Index
is still a table and that you always need to use row
and column
as indices for a table. I tried the following code (without the intermediate table Index
) and it works, although I'm not sure this is the best solution.
[i,j] = ind2sub(size(T),find(isnan(T{:,:})));
T(i,j) = {0};
One way is to convert to an array, do the substitution with logical indexing, and then go back to a table:
T = table(rand(200,1),[rand(199,1);NaN],rand(200,1));
t = table2array(T);
t(isnan(t)) = 0;
T = array2table(t, 'VariableNames', T.Properties.VariableNames);
Also, consider using just the array if that's acceptable for you (without converting from or to a table).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.