Getting average value of grid point data

조회 수: 4 (최근 30일)
Yen Su
Yen Su 2021년 1월 26일
댓글: Yen Su 2021년 1월 29일
I have a grid point data column in the form of 0000 where first two digits is x-coordinates and last two digits is y-coordinates as shown in filename 'gridid.xlsx'. For each grid point data column, I have corresponding snow depth data column. The data is for 10 years.
I have locationid with x-coordinate and y-coordinates as shown in 'location.xlsx'. The locationid is located anywhere inside the grid. I would like to run for loop for each year and get a average snow depth for each locationid. The average is calculated from four nearest corner grid point snow depth data in which locationid is located. Could anybody help me to figure it out what is the necessary steps to do? Any advice is highly appreciated.
  댓글 수: 5
David Hill
David Hill 2021년 1월 27일
Average of 0000,0001,0100,0101 would correlate to average snow depth for 0000?
dpb
dpb 2021년 1월 27일
If a grid is (one-digit, not two)...
00 01 02 03 ...
10 11 12 13 ...
20 21 22 23 ...
30 31 32 33 ...
...
and want to average blocks of four, the simplest would be to build the 2D array of accumulations by location and then use convolution or 2D filter or, if have Image Proc toolbox, blockproc
While they're randomaly arrange, it'll be slow and a pain to code...

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2021년 1월 27일
a=readtable('gridid.xlsx');
snowgrid=zeros(9,9,10);
for k=1:10
c=zeros(10);
i=ismember(a.year,num2str(k));
b=cell2mat(a.gridid(i))';
x=str2num(b(1:2,:)')+1;
y=str2num(b(3:4,:)')+1;
idx=sub2ind([10,10],x,y);%if the idx does not change, this could be a constant outside the loop
c(idx)=a.snowDepth(i);
c=movsum(c,2);
c=movsum(c(2:end,:)',2);
snowgrid(:,:,k)=c(2:end,:)'/4;%produces average snow per year at each grid location
end
b=readtable('location.xlsx');
for k=1:10
c=snowgrid(min(ceil(b.X_coordinate(k)),9),min(ceil(b.Y_coordinate(k)),9),:);%location data above 9 is outside the grid
averageSnowAtgrid(k,:)=c(:)';%produces matrix of average snow at grid location (row) per year (column)
end
  댓글 수: 5
David Hill
David Hill 2021년 1월 28일
t=array2table(averageSnowAtgrid);
t.Properties.VariableNames={'year1','year2','year3','year4','year5','year6','year7','year8','year9','year10'};
t.Properties.RowNames={'id1';'id2';'id3';'id4';'id5';'id6';'id7';'id8';'id9';'id10'};
display(t);
Yen Su
Yen Su 2021년 1월 29일
Hi David,
Is there a way to dynamically assign the variable names for the above code?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by