MATLAB: Regriding an irregular matrix

regridding

Dear all,
I have a 3D irregular matrix in time and position below :
A = 3000 x 3
Column1 = time (vary from 2002-2005)
Column2 = Longitude (vary from 40-50)
Column3 = Value
A = [2Jan2002 40 65
6Jan2002 45 22
8Jan2002 43 45
8Jan2002 49 64
30Jan2002 42 78
4Feb2001 43 56
6Feb2001 47 67
: : :
23Nov2005 44 76
3Dec2005 42 89
5Dec2005 48 67
9Dec2005 44 78
13Dec2005 41 89
19Dec2005 49 34
23Dec2005 45 67
24Dec2005 43 88
31Dec2005 47 34];
I want to regridding this irregular data in time and position into regular matrix based on monthly (2002-2005; so 48 month) and 1 degree lon position (40-50; so 10 points), in summary the output mtrix will be 10×48.
Some points longitude will have NaN value cause no record in this months at x longitude.
Anyone can help?
Best regards,

Best Answer

If you have data as text file - yourdata.txt (eg):
2Jan2002 40 65
6Jan2002 45 22
8Jan2002 43 45
8Jan2002 49 64
30Jan2002 42 78
4Feb2002 43 56
6Feb2002 47 67
23Nov2005 44 76
3Dec2005 42 89
5Dec2005 48 67
9Dec2005 44 78
13Dec2005 41 89
19Dec2005 49 34
23Dec2005 45 67
24Dec2005 43 88
31Dec2005 47 34
please try this is code:
f=fopen('yourdata.txt');
c = textscan(f,'%s %f %f');
fclose(f);
[y,m] = datevec(c{1});
[m1,y1] = ndgrid(1:12,2002:2005);
[i2,i2] = ismember([y,m],[y1(:),m1(:)],'rows');
[j2,j2] = ismember(round(c{2}),40:50);
out = accumarray([i2,j2],c{3},[48,10],@mean,nan); % [EDITED]