I wish to calculate the mean values of temperature over the latxlon grid and only for 12GMT for all the 65 months using matlab code.

I have a data with dimension (lat,lon,time,months) as (5,5,2,65)
number of latitude =5
number of longitude =5
time = 1 for 00 GMT and 2 for 12 GMT
number of months 65
I wish to calculate the mean values of temperature over the latxlon grid and only for 12GMT for all the 65 months using matlab code.
Any suggestions would be highly appreciated.
regards,
Devendra

Answers (2)

Hi
You can take the mean of the data as such
latxlon = randi([20 40],5,5,2,65);
meanTemp = mean(latxlon(:,:,2,:));
If you want to take the mean over all 65 months, you can take the mean along the 4th dimension.
meanTemp = mean(latxlon(:,:,2,:),4)
meanTemp = 5×5
29.7692 30.6923 29.1538 30.1231 29.3538 29.8923 31.0308 30.4000 28.9077 28.4154 29.9538 31.0308 30.4462 30.4308 30.8154 30.7077 29.6462 30.3077 30.2769 29.5692 30.4308 30.8154 29.6154 29.0769 29.9846
Here is a link to the documentation
Hope this helps!

4 Comments

THank you very much for your help. However, these matlab code gives mean value of temperature with dimension (65,5). I want only 65 values for 65 months. Further, Now I want to commput the mean value of total precipitation over the latxlon grid and also for both time 1(00GMT) and 2(12GMT). Where should I assign variable total precipitation in the following lines
latxlon = randi([20 40],5,5,2,65);
meanTemp = mean(latxlon(:,:,2,:));
Once again thanks a lot.
Devendra
I have created a latxlon grid based on the dimensions you provided as [5,5,2,65] and assuming that grid were values of temperatures. It would help if you provided the data for this grid to calculate the same for total precipitation.
As for obtaining 65 values for 65 months. You could take the mean temperature as such
meanTemp= mean(latxlon(:,:,2,:),[1 2]);
The latxlon(:,:,2,:) takes only the '12 GMT' values from the latxlon grid, hence the grid now has dimensions [5, 5, 1 ,65]. The [1 2] argument would compute mean over each page of data (rows and columns). This matrix should now have dimensions [1, 1, 1, 65], which is the 65 values for 65 months you needed. You can further reshape the meanTemp matrix to 65*1 matrix as such:
meanTemp = reshape(meanTemp,65,1);
If you had a total precipitation with the same dimensions as the temperature grid, you could compute the mean the same way by replacing the variable latxlon in the above code. Do refer to the documentation for the mean function (previously attached) if you need to make any tweaks.
I have attached the netcdf matlab code to read the data and netcdf input file. I want to calculate the mean values of nine varaibles over lat x lon x time x 6961 (4,4,2,6961) for each variable. I request you to kindly modify the code to get the required data. Thanks a lot for your help.
Sanchit
The dimension of output file should be (3480,9).
Sanchit

Sign in to comment.

Products

Release

R2023a

Asked:

on 29 Jun 2023

Answered:

on 20 Jul 2023

Community Treasure Hunt

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

Start Hunting!