Selection months (winter) in a matrix

4 visualizzazioni (ultimi 30 giorni)
Hi all,
I have some problems with my datas. I have a matrix of 20x6x12x32 where 20 and 6 are the longitude and latitude, 12 the months and 384 the years.
I need to select the winter months like DJF, so I need the december of the previous year. For the first year I will obtain a winter just with JF and for the last year just with December.
I try this
winter = wind(:,:,[12,1,2],:);
for i=1:20
for j=1:6
for k=1:3
for z=1:32
winter_mean(i,j,z)=mean(winter(i,j,k,z));
end
end
end
end
But the result is incorrect because it choose the December of that year and not the previous one.
Any ideas?? Thank you really much
  1 Commento
Walter Roberson
Walter Roberson il 18 Gen 2021
Why are you taking mean() of a scalar? Your i, j, k, and z are all scalars, so winter() indexed there is a scalar, and mean() of a scalar is the same scalar.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 18 Gen 2021
maxyear_to_calculate = 32;
winter = permute(wind(:,:,[12,1,2],:), [3 1 2 4]); %month, long, lat, year
nyear = size(winter,4);
max_middle_year = min(maxyear_to_calculate, nyear);
for i=1:20
for j=1:6
z = 1;
winter_mean(i, j, z) = mean(winter(2:3, i, j, z)); %first year has no december
for z = 2 : max_middle_year
winter_mean(i,j,z) = mean([winter(1, i, j, z-1); winter(2:3, i, j, z)]);
end
if maxyear_to_calculate == nyear+1
z = maxyear_to_calculate;
winter_mean(i,j,z) = winter(1, i, j, z-1); %last year has only december
end
end
end
Here, you should set maxyear_to_calculate to 384+1 = 385 if you want the final year to be calculated for.
That is, I respected your only calculating to 32, but at the same time putting in the full logic to calculate for the final year. The final entry is an extra entry; for example if you had data for 1970 1971 1972 then the entries would be based upon (JF 1970), (D 1970, JF 1971), (D 1971, JF 1972), (D 1972) for a total of 4 entries for data years.
The permute() was added to make it easier to calculate the means: without that re-arrangement, winter(i,j,2:3,z) would be 1 x 1 x 3 x 1 and I didn't want to bother with mean(cat(3,winter(i,j,1,z-1),winter(i,j,2:3,z)),3) so I moved the months to the first dimension.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by