Asked by m0xty Wilopo
on 7 May 2013

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 10x48.

Some points longitude will have NaN value cause no record in this months at x longitude.

Anyone can help?

Best regards,

*No products are associated with this question.*

Answer by Andrei Bobrov
on 8 May 2013

Edited by Andrei Bobrov
on 10 May 2013

Accepted 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]

Answer by m0xty Wilopo
on 8 May 2013

Yes, the date is datenum precisely, I just want to show that the first column is date in string. It is in numeric.

Answer by Cedric Wannaz
on 8 May 2013

Edited by Cedric Wannaz
on 8 May 2013

A general approach could be the following, assuming that you have this matrix `A` with datenums in the first row.

[y, m] = datevec(A(:,1)) ;

dateId = ((y - min(y))*12 + m).' ; nYears = range(y) + 1 ;

lonId = A(:,2) - min(A(:,2)) + 1 ; nLon = range(A(:,2)) + 1 ; % You might want to fix this % instead of having it flexible. dataArray = nan(nLon, nYears*12) ; ind = sub2ind(size(dataArray), lonId, dateId) ; dataArray(ind) = A(:,3) ;

Now, as mentioned in my comment, we should improve it a bit if you can have cases where there are multiple values for the same pairs dateId/lonId. Just let me know and we can extend it using ACCUMARRAY.

Answer by m0xty Wilopo
on 9 May 2013

Dear Cedric and Andrei :)...Many THANKS! As usual it was really helpful. However if I have data at same longitude, I want to averaging it (mean) how to do that ie:

2Jan2002 40 65 6Jan2002 45 22 ---> So I will have value 24 instead of 48 7Jan2002 45 26 8Jan2002 43 45 8Jan2002 43.1 51 ---> result will be 48 at lon 43 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

How to do that? Andrei giving answer using accumarray in my previous question but it was for another data set in same longitude. But now I want averaging it at multi longitude/position.

Opportunities for recent engineering grads.

## 1 Comment

## Cedric Wannaz (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/74920#comment_147876

This is not a valid array as it mixes strings and numeric values. How are your data truly stored at this point? If you have time, longitude, and value, you just need a 2D array, as you are mentioning later in the question when you define the dimension of your final array. Do you have in fact something like a 3 columns CSV or Excel file with the original data? If so, assuming that we can treat "31Dec2005"-like timestamps correctly, how do you want to summarize values on a per month basis; is it a simple average?