Reputation: 1269
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?
Upvotes: 1
Views: 2099
Reputation: 112659
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).
Upvotes: 1
Reputation: 2334
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};
Upvotes: 4