在Matlab中求平均值,同时排除NaN和其他特定值

时间:2016-10-25 20:05:48

标签: excel matlab

我有一个Excel电子表格,其中的值列表示实验设置中的不同变量。例如,我的数据中的一列可能被称为"反应时间"因此包含代表时间的值,以毫秒为单位。如果在试验期间出现问题并且没有记录反应时间的值,则Matlab称之为“NaN。”#34;我知道我可以使用:

File file = new File("C:\\Users\\lucas\\Desktop\\file-with-800MB.log");

    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    try {           
        while (it.hasNext()) {
            String line = it.nextLine();
            // do something with line, here just sysout...
            System.out.println( line );
        }
    } finally {
        LineIterator.closeQuietly(it);
    }

这将返回"反应时间"中列出的平均值。我的电子表格栏目(第3栏)。它会跳过任何不是数字的东西(NaN,在实验过程中出现错误)。

这是我需要帮助的地方:

除了排除NaN之外,我还需要省略一些值。例如,一种类型的错误导致打印" 1 ms"反应时间,因此打印在电子表格中。如何指定我需要省略NaN," 1"以及任何其他值?

提前致谢, 米奇

1 个答案:

答案 0 :(得分:0)

在您使用NaN mean之前,您可以尝试使用the standardizeMissing函数替换要用'omitnan'排除的值。例如:

>> x = 1:10;
>> x = standardizeMissing(x, [3 4 5]); % Treat 3, 4, and 5 as missing values
x =
     1     2   NaN   NaN   NaN     6     7     8     9    10
>> y = mean(x, 'omitnan');

如果您将Excel工作表读入表格,standardizeMissing只能在您关注的列中使用NaN替换值,如果您使用DataVariables名称 - 值对。< / p>