Average based on logical mask and condition
이전 댓글 표시
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
Image Analyst
2018년 11월 5일
Any diagram, image, or screenshot to help us visualize this?
JohnGalt
2018년 11월 6일
you could try make a smaller and more general example... one where you can verify the solution quickly... e.g.
% given a large grid:
a = floor(magic(9)/20);
% find the avg of all possible 3x3 grids which don't contain zeros
one (probably slow) solution to this is:
for i = 1:size(a,1)
for j = 1:size(a,2)
if i+2<=size(a,1) & j+2<=size(a,2)
submat = a(i:i+2,j:j+2);
if any(submat(:)==0)
out(i,j) = nan;
else
out(i,j) = mean(submat(:));
end
end
end
end
disp(out)
but the problem is now easy for others to understand... and all you are looking for now is a faster solution :)
Bruno Luong
2018년 11월 6일
편집: Bruno Luong
2018년 11월 6일
"Please find the mat file attached."
Anyone can see it? I don't.
nlm
2018년 11월 6일
nlm
2018년 11월 6일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!