How to average over multiple dimensions at varying increments?

1 Ansicht (letzte 30 Tage)
cgs5198
cgs5198 am 20 Mär. 2018
Beantwortet: BhaTTa am 17 Okt. 2024
Hello! I have a 144x73x841 array of globally gridded temperature data. For my purposes, I need to shrink this data to a 144x73x719 matrix to fit my other data I have, but that is simple enough to do.
The 144x73 part of the matrix is latitude and longitude data (I do not remember which is which exactly, but that does not matter).
The other dimension is just time, where each of the 841 represents a month, officially in hours since 1800 (first value is 1297320.0).
What I need is this: a simple way to, essentially, find globally averaged temperature, yearly, for this dataset in question. I am having trouble figuring out which way to splice my arrays accordingly and whatnot when it comes to performing the averaging attempts I have made. Any help is appreciated. Thanks in advance!

Antworten (1)

BhaTTa
BhaTTa am 17 Okt. 2024
Hey @cgs5198, I assume that in the array dimensions of 144x73x841, 144 and 73 represent spatial dimensions (latitude and longitude), and 841 represents time in months.
Below I have provided implemenation to compute globally averaged yearly temperatures from your 3D array of temperature data
temperatureData = rand(144, 73, 841);
% Calculate the number of complete years
monthsPerYear = 12;
numCompleteYears = floor(size(temperatureData, 3) / monthsPerYear);
yearlyGlobalAverages = zeros(1, numCompleteYears);
for yearIdx = 1:numCompleteYears
startMonth = (yearIdx - 1) * monthsPerYear + 1;
endMonth = yearIdx * monthsPerYear;
yearlyData = temperatureData(:, :, startMonth:endMonth);
% Compute monthly global averages
monthlyGlobalAverages = squeeze(mean(mean(yearlyData, 1), 2)); % Average over lat and lon
% Compute the yearly average temperature
yearlyGlobalAverages(yearIdx) = mean(monthlyGlobalAverages);
end
disp('Yearly Global Averages:');
disp(yearlyGlobalAverages);
Hope it helps.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by