简体   繁体   English

MATLAB将每日数据的3D数组分割为每月细分

[英]MATLAB Cut 3D array of daily data into monthly segments

I have a 1437x159x1253 large matrix (let's call it A) of daily sea ice data for a little over 2 years. 我有一个1437x159x1253大型矩阵(我们称之为A),用于2年多一点的每日海冰数据。 I need to write a code that takes the daily data from each month and does mean(A, 3) on it. 我需要编写一个代码,获取每个月的每日数据,并在其上表示(A,3)。 So basically, 1253 is the t in days. 因此,基本上,1253是天数。 If I start from January, I need to do mean(A,3) of the first 31 days, then the mean(A,3) of February, the next 28 or 29 days. 如果我从一月开始,则需要做前31天的平均值(A,3),然后是接下来28或29天的二月的平均值(A,3)。 Because the days alternate between 31 and 30 (and 28 or 29 for February), I don't know how to write a code to do this. 由于日期在31到30(2月为28或29)之间交替,因此我不知道如何编写代码来执行此操作。 I can do it manually, but that would take a while. 我可以手动完成,但这需要一段时间。

Thanks! 谢谢!

You can initialize an array containing the number of days in each month, Mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] using boolean to check whether it's a leap year (to set Mon(2) = 29 ). 您可以初始化一个包含每个月Mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]数的数组, Mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]使用布尔值检查是否为leap年(设置Mon(2) = 29 )。 The number of days will help you index each month appropriately, using a loop like: 天数将帮助您使用类似下面的循环来每月适当地建立索引:

index=1;
for i=1:12
  average = mean(A(:,:,index:(index+Mon(i)-1),3);
  index = index+M(i);     % Starting location of the next month
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM