フィルターのクリア

EDITED: Variance when only one observation is not NaN

2 ビュー (過去 30 日間)
Mia Dier
Mia Dier 2021 年 1 月 12 日
編集済み: Matt J 2021 年 1 月 12 日
EDITED: I think my problem was not clear from the previous example. So, I added a new one below here:
I am trying to calculate a moving variance of the max last 5 years of observations. If remove 'omitnan' then I get NaN for pastvarofA for all the next 5 observations that follows NaN and I don't want that.
I get 0 if there is an observation that is non NaN in the sample that the variance is calculated. But I want to get NaN when that is the case.
A=[4;NaN;5;5;6;8;9];
pastvarofA=NaN(7,1);
for i=2:5
for k=i+1:7
varofA(k,1)=var(A(k-i:k-1), 'omitnan');
end
end
What I want to get is something like this:
pastvarofA
NaN
NaN
Var of 4 and NaN=NaN
Var of 4, NaN and 5= Var of 4 and 5
Var of 4, NaN, 5 and 5= Var of 4, 5 and 5
Var of 4, NaN, 5, 5 and 6 = Var of 4, 5, 5 and 6
Var of NaN, 5, 5, 6 and 8 = Var of 5, 5, 6 and 8
But what I get is this:
parvarA:
NaN
NaN
Var of 4 and NaN= 0
Var of 4, NaN and 5= Var of 4 and 5
Var of 4, NaN, 5 and 5= Var of 4, 5 and 5
Var of 4, NaN, 5, 5 and 6 = Var of 4, 5, 5 and 6
Var of NaN, 5, 5, 6 and 8 = Var of 5, 5, 6 and 8
  2 件のコメント
Ive J
Ive J 2021 年 1 月 12 日
If so, why are you using omitnan then?!
Mia Dier
Mia Dier 2021 年 1 月 12 日
It was probably not clear from previous example why removing 'omitnan' didn't solve the problem. I added a new example above and I hope that the problem is clear now!

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

採用された回答

Matt J
Matt J 2021 年 1 月 12 日
編集済み: Matt J 2021 年 1 月 12 日
A=[1;2;3;NaN;5;5;6];
varofA = movvar(A,2,'Endpoints','discard')
varofA = 6×1
0.5000 0.5000 NaN NaN 0 0.5000
  1 件のコメント
Matt J
Matt J 2021 年 1 月 12 日
編集済み: Matt J 2021 年 1 月 12 日
For your new example,
A=[4;NaN;5;5;6;8;9];
pastvarofA=movvar(A,[4,0],'omitnan');
pastvarofA=[nan;pastvarofA(1:end-1)]
pastvarofA = 7×1
NaN 0 0 0.5000 0.3333 0.6667 2.0000

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

その他の回答 (1 件)

Catalytic
Catalytic 2021 年 1 月 12 日
Omit 'omitnan'
  1 件のコメント
Mia Dier
Mia Dier 2021 年 1 月 12 日
It was probably not clear from previous example why removing 'omitnan' didn't solve the problem. I added a new example above and I hope that the problem is clear now!

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

カテゴリ

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