Omit nans from data set when calculating weights
6 ビュー (過去 30 日間)
古いコメントを表示
Hello!
I currently have data from 5 rain gages and am trying to calulated weighted averages for each to better represent how much rain fell in certain areas. Some of the rows have NaNs in them so when I calulate the weighted average across the five gages the product = NaN. I would like to ignore the NaNs in the calculation but cannot figure out how to do this. I have tried the 'omitnan' but maybe I am not putting it in the right place? Thank you in advance!!
%Doing all precipitation
Precip1 = table2timetable(P1);
Precip2 = table2timetable(P2);
Precip3 = table2timetable(P3);
Precip4 = table2timetable(P4);
Precip5 = table2timetable(P5);
AllPrecip=synchronize(Precip1, Precip2,Precip3,Precip4,Precip5);
Weight_HQ=[0.003365661397
0.1528054559
0.3846818121
0.2268942917
0.2322527789];
Weight_N1B=[0
0
0.01912681913
0.9064449064
0.07442827443]
Weight_N2B=[0
0.8899179677
0.1100820323
0
0]
Weight_N4D=[0
0.03316776158
0.8987341772
0.06809806121
0]
Weight_N20B=[0
0
0.001316655695
0.6356155365
0.3630678078]
AllPrecip.HQ_Precip=Weight_HQ(1)*AllPrecip.ppt_Precip1+Weight_HQ(2)*AllPrecip.ppt_Precip2+Weight_HQ(3)*AllPrecip.ppt_Precip3+Weight_HQ(4)*AllPrecip.ppt_Precip4+Weight_HQ(5)*AllPrecip.ppt_Precip5;
AllPrecip.N1B_Precip=Weight_N1B(1)*AllPrecip.ppt_Precip1+Weight_N1B(2)*AllPrecip.ppt_Precip2+Weight_N1B(3)*AllPrecip.ppt_Precip3+Weight_N1B(4)*AllPrecip.ppt_Precip4+Weight_N1B(5)*AllPrecip.ppt_Precip5;
AllPrecip.N2B_Precip=Weight_N2B(1)*AllPrecip.ppt_Precip1+Weight_N2B(2)*AllPrecip.ppt_Precip2+Weight_N2B(3)*AllPrecip.ppt_Precip3+Weight_N2B(4)*AllPrecip.ppt_Precip4+Weight_N2B(5)*AllPrecip.ppt_Precip5;
AllPrecip.N4D_Precip=Weight_N4D(1)*AllPrecip.ppt_Precip1+Weight_N4D(2)*AllPrecip.ppt_Precip2+Weight_N4D(3)*AllPrecip.ppt_Precip3+Weight_N4D(4)*AllPrecip.ppt_Precip4+Weight_N4D(5)*AllPrecip.ppt_Precip5;
AllPrecip.N20B_Precip=Weight_N20B(1)*AllPrecip.ppt_Precip1+Weight_N20B(2)*AllPrecip.ppt_Precip2+Weight_N20B(3)*AllPrecip.ppt_Precip3+Weight_N20B(4)*AllPrecip.ppt_Precip4+Weight_N20B(5)*AllPrecip.ppt_Precip5;
0 件のコメント
採用された回答
Voss
2024 年 4 月 11 日
You can use fillmissing to replace any NaN with 0, so that it doesn't contribute to the weighted sum.
Give the resulting timetable a different name if you want to preserve your original timetable.
T = fillmissing(AllPrecip,'constant',0);
AllPrecip.HQ_Precip = Weight_HQ(1)*T.ppt_Precip1+Weight_HQ(2)*T.ppt_Precip2+Weight_HQ(3)*T.ppt_Precip3+Weight_HQ(4)*T.ppt_Precip4+Weight_HQ(5)*T.ppt_Precip5;
% etc. for the other calculated columns
3 件のコメント
その他の回答 (1 件)
Star Strider
2024 年 4 月 11 日
Since NaN values are considered to be ‘missing’, to remove them from a specific vector (or matrix), you can use the rmmissing function.
4 件のコメント
Star Strider
2024 年 4 月 11 日
編集済み: Star Strider
2024 年 4 月 11 日
My pleasure!
Note —
x = randi(9, 1, 5);
x([2 4]) = NaN
w = 1:numel(x)
x = fillmissing(x, 'Constant',0)
mean_xw = mean(x.*w) % Counts Zeros As Vector Elements
x([2 4]) = NaN
correct_mean_xw = mean(x.*w,'omitnan') % Omits 'NaN' Values
EDIT — (11 Apr 2024 at 22:08)
Added illustration.
.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!