regriding an irregular matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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,
1 commentaire
Cedric
le 7 Mai 2013
Modifié(e) : Cedric
le 7 Mai 2013
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?
Réponse acceptée
Andrei Bobrov
le 8 Mai 2013
Modifié(e) : Andrei Bobrov
le 10 Mai 2013
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]
Plus de réponses (3)
Cedric
le 8 Mai 2013
Modifié(e) : Cedric
le 8 Mai 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!