how to do mean of a column with a loop and excluding nans?

1 回表示 (過去 30 日間)
tonyrodriguez
tonyrodriguez 2018 年 3 月 16 日
コメント済み: Birdman 2018 年 3 月 16 日
so i have this large file that is contains nans randomly, its a 1140 X 1 matrix i want to do a for loop that excludes all the nans and do the mean of that can someone explain me how to do this?
  1 件のコメント
tonyrodriguez
tonyrodriguez 2018 年 3 月 16 日
i cant use nanmean for this

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

採用された回答

Birdman
Birdman 2018 年 3 月 16 日
編集済み: Birdman 2018 年 3 月 16 日
You do not need a for loop. Matlab's mean function has an option of omitnan. You can use it as follows:
res=mean(a,'omitnan')
  4 件のコメント
tonyrodriguez
tonyrodriguez 2018 年 3 月 16 日
that works thank you! .. but can you explain me what the res do? can you do without it? and just count? since in my class we have only used count... and perhaps do at the end mean(isnan(a(i)) ?
Birdman
Birdman 2018 年 3 月 16 日
res is used to sum up the variables that are not NaN, that is it. You need a variable to sum up the variables that are not NaN in a for loop, therefore you need it and cnt simply counts how many times that if statement is entered.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by