MATLAB: Double loop formation question

loops

Hello,
I have a doubt about the possibility of forming another loop of following repetation (the size of GWdepth is 74*100*1566, 74 and 100 indicate the lon and lat):
j = 1
for i = 1:30
depth_week_j(:,:,i) = GWdepth(:,:,j);
j = j + 52
end
mean_depth_week_j(:,:,1) = nanmean(depth_week_j,3);
j = 2
for i = 1:30
depth_week_j(:,:,i) = GWdepth(:,:,j);
j = j + 52
end
mean_depth_week_j(:,:,2) = nanmean(depth_week_j,3);
…… I need to repeat this process for 52 times until j = 52,
j = 52
for i = 1:30
depth_week_j(:,:,i) = GWdepth(:,:,j);
j = j + 52
end
mean_depth_week_j(:,:,52) = nanmean(depth_week_j,3);
Any ideas on how to form a double loop based on this condition?

Best Answer

for j=1:52
jj=j;
for i = 1:30
depth_week_j(:,:,i) = GWdepth(:,:,jj);
jj = jj + 52;
end
mean_depth_week_j(:,:,j) = nanmean(depth_week_j,3);
end