MATLAB: Hello everyone! I need your valuable help here.

subsetting the third dimension in a 3d matrixtext file

I have a 3D matrix (12 x 14 x 360) which is 30 years monthly data. I need only months from Apri to September from all 30 years. The output matrix I expect is 12 x 14 x 180. I am getting this error: “Subscripted assignment dimension mismatch.” Correct me please.
% code
h=1:180
for year=1:30;
April=(year-1)*12+4;
Sept=(year-1)*12+9;
seasons(:,:,h)=mat(:,:,April:Sept);
end
Kindly, Emgdaw

Best Answer

April = 4;
Sept = 9;
S = size(mat);
seasons = reshape(mat,[12 14 12 30]);
seasons = seasons(:,:,April:Sept,:);
seasons = reshape(seasons,[12,14,30*(Sept-April+1)]);
Simple, really. The reshape allows you to extract those months. Then reshape things again, to return the array into the desired shape. Or, I could have left it as an array of size [12,14,6,30]. That may help you to use it later. It depends on what you are doing.