Average IF NaN counting

2 ビュー (過去 30 日間)
Osnofa
Osnofa 2017 年 11 月 6 日
編集済み: Osnofa 2017 年 11 月 6 日
Hi,
I have a 20*207 matrix which contains numbers and NaN.
I need to average each column if the amount of NaN is <5. If the number of NaN is >=5 then I would like to remove that column from the matrix. How can I do it? I've been trying some stuff but I'm not able to achieve it.
thanks.
  1 件のコメント
Geoff Hayes
Geoff Hayes 2017 年 11 月 6 日
Afonso - what have you been trying? Please copy and paste your code to this question so that we can get an idea of what you have attempted and determine why it is not working. Presumably you are using isnan to determine which elements in your column are NaN...

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

採用された回答

Robert
Robert 2017 年 11 月 6 日
Some test data
x = randn(20, 207);
x(randi(numel(x), 800, 1)) = nan;
Identify the NaNs with isnan and count the number per column with sum. Then select the columns that have fewer than 5 NaNs.
good_enough = sum(isnan(x)) < 5;
x = x(:, good_enough);
And use nanmean to take the average of the remaining columns.
avg = nanmean(x);
If you don't have the stats toolbox, try the following
the_nans = isnan(x);
num_nans = sum(isnan(x));
good_enough = num_nans < 5;
x = x(:, good_enough);
the_nans = the_nans(:, good_enough);
num_nans = num_nans(:, good_enough);
x(the_nans) = 0;
avg = sum(x);
avg = avg ./ (20 - num_nans);
  1 件のコメント
Osnofa
Osnofa 2017 年 11 月 6 日
編集済み: Osnofa 2017 年 11 月 6 日
Thank you Robert! way simplier than I thought.
I tried the 1st method.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by