Omit nans from data set when calculating weights
古いコメントを表示
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;
採用された回答
その他の回答 (1 件)
Star Strider
2024 年 4 月 11 日
1 投票
Since NaN values are considered to be ‘missing’, to remove them from a specific vector (or matrix), you can use the rmmissing function.
4 件のコメント
Callie
2024 年 4 月 11 日
Star Strider
2024 年 4 月 11 日
編集済み: Star Strider
2024 年 4 月 11 日
My pleasure!
Without your data, it is not easy to suggest a specific alternative. Probably the best option is to iterate through each row and use @(x)~isnan(x).
Perhaps something like this —
AllPrecipEdited = structfun(@(x)x(~isnan(x)), AllPrecip);
The problem with setting the NaN values to a speciffic value is that zeros (or whatever finite value you set them to) will be counted in calculating the mean or other statistics. If you simply eliminate the NaN values, they will not be considered in those calculations.
.
Callie
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.
.
カテゴリ
ヘルプ センター および File Exchange で NaNs についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!