将表中的NaN替换为0 [英] Replace NaN's in table with 0

查看:432
本文介绍了将表中的NaN替换为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab R2015b中有一个table,其中包含以下数据

I have a table in matlab R2015b with the following data

                var 1        var 2       var 3      
第1行NaN         2
第2行2                                    4           NaN

                var 1         var 2        var 3       
Row1         1             NaN           2
Row2         2                4            NaN

我想将表中的所有NaN都替换为0-因此需要遵循以下几条原则:

I'd like to replace all NaN's in my table with 0's - so something along these lines:

假设我的桌子叫A

newTableA = rowfun(@(x) x(isnan(x)) = 0,A,'ExtractCellData',true);  

我想我可以将表转换为新矩阵B,执行B(isnan(B))= 0,然后转换回表,但是我认为可能会有更有效的方法.

I suppose I could convert my table to a new matrix B, perform, B(isnan(B)) = 0, and convert back to a table, but I thought there might be a more efficient way.

推荐答案

只需遍历变量:

t = array2table([1 nan 2; 2 4 nan])
for i=1:size(t,2)
    x = t{:,i};
    x(isnan(x)) = 0;
    t{:,i} = x;
end

如果您签出以下方法的源代码:

If you checkout the source code of methods like:

>> which table\ismissing
>> which table\standardizeMissing

无论如何,您会发现这或多或少是他们所做的...

you'll see that is more or less what they do anyway...

这篇关于将表中的NaN替换为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆