[英]Remove NaN from all variables in table in Matlab
I have a table with many variables and some of the entries have NaN
´s. 我有一个包含许多变量的表,并且某些条目具有
NaN
。 I would like to replace all NaN
with 0. I am able to find the NaN
´s with this: 我想将所有
NaN
都替换为0。我可以用以下形式找到NaN
:
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:
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. 问题似乎在于
Index
仍然是一个表,并且您始终需要使用row
和column
作为表的索引。 I tried the following code (without the intermediate table Index
) and it works, although I'm not sure this is the best solution. 我尝试了以下代码(没有中间表
Index
),并且可以工作,尽管我不确定这是否是最佳解决方案。
[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). 另外,如果您可以接受的话,请考虑仅使用数组(无需在表之间进行转换)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.