Ignore NaN and -9999 values

1 回表示 (過去 30 日間)
SuzieChan
SuzieChan 2020 年 4 月 10 日
コメント済み: BN 2020 年 4 月 10 日
Hi,
I have a table of data. There are 5 columns. 2000 rows.
Some data has NaN, 0 and -9999 values.
I want to calculate the average of each column, ignoring NaN, 0 and -9999 values.
What code can do this?
Thank you.

回答 (2 件)

BN
BN 2020 年 4 月 10 日
編集済み: BN 2020 年 4 月 10 日
Hi, It should be something like this, I learned it yesterday.
I assume T is the name of your table:
T_new = standardizeMissing(T, 0);
T_new = standardizeMissing(T_new, -9999)
A = T_new(:,vartype('numeric'));
omean = @(x) mean(x,'omitnan'); %ignoring NaN
mean_values = varfun(omean,A)
  2 件のコメント
Steven Lord
Steven Lord 2020 年 4 月 10 日
You don't need to call standardizeMissing twice. It accepts a vector of values that should be converted to the standard missing value (which is NaN for double precision arrays.) Using a slightly modified version of the first example from its documentation page:
A = [0 1 5 -99 8 3 4 -99 16];
B = standardizeMissing(A,[-99, 0]);
beforeAndAfter = [A; B]
The 0 and the two -99 values in A were replaced by NaN in B.
BN
BN 2020 年 4 月 10 日
Thank you for let me know.

サインインしてコメントする。


Ameer Hamza
Ameer Hamza 2020 年 4 月 10 日
編集済み: Ameer Hamza 2020 年 4 月 10 日
% generting an example table
t = [1 2 3 4 5;
0 2 1 3 -9999;
-9999 5 7 0 9;
nan 1 2 3 -9999];
T = array2table(t);
values = T.Variables;
mask = isnan(values) | values==0 | values==-9999;
values(mask) = nan;
result = nanmean(values, 1)
  1 件のコメント
BN
BN 2020 年 4 月 10 日
+1

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by