How to average over multiple dimensions at varying increments?
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
BhaTTa
2024-10-17
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!