Average based on logical mask and condition

7 ビュー (過去 30 日間)
nlm
nlm 2018 年 11 月 5 日
コメント済み: nlm 2018 年 11 月 6 日
I want to average the data according to unique ID and a logical mask. I have 3 km logical mask which will average into perfect 36 km grid. I average into 36 km based on compartmentID I created. I'm having trouble, when I want to discard those 36 km grids when one of the 3 km grid is zero (based on logical mask ID). Size of the matrix is 406, 964 for 36 km grid which perfectly fit 4872, 11568 3 km grids.
%Form the ID's for 3 km grid so to average for 36 km grid.
rowId = ceil( (1 : 4872) / 12 );
colId = ceil( (1 : 11568) / 12 );
[colID, rowID] = meshgrid( colId, rowId );
compartmentID = colID + (rowID - 1) * max(colId);
Average = reshape(accumarray(compartmentID(:),reshape(Grid_3km,[],1), [],@mean,NaN ), 406, 964);
Can anyone help me with the condition, that says if one of the 3 km grid in 36 km is zero then discard the average.
  6 件のコメント
nlm
nlm 2018 年 11 月 6 日
Sorry, I could not attach it. It was larger than 5 Mb.
nlm
nlm 2018 年 11 月 6 日
@JohnGalt Can you please elaborate ?

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

採用された回答

Bruno Luong
Bruno Luong 2018 年 11 月 6 日
Just put NaN on the 3km-grid at the place you want to discard
Grid_3km_Masked = Grid_3km;
Grid_3km_Masked(YourMask==0) = NaN;
then by using your working code on Grid_3km_Masked, it will propagate to the average on coarse-grid data result:
%Form the ID's for 3 km grid so to average for 36 km grid.
rowId = ceil( (1 : 4872) / 12 );
colId = ceil( (1 : 11568) / 12 );
[colID, rowID] = meshgrid( colId, rowId );
compartmentID = colID + (rowID - 1) * max(colId);
Average = reshape(accumarray(compartmentID(:),reshape(Grid_3km_Masked,[],1), [],@mean,NaN ), 406, 964);
  10 件のコメント
Bruno Luong
Bruno Luong 2018 年 11 月 6 日
Do you mind to post the final working version of the code? I'm curious to see what you want to compute.
nlm
nlm 2018 年 11 月 6 日
I got with the code you suggested, I just needed to transpose the matrix. Mask thing did the trick. Thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by