# Hello everyone! I need your valuable help here.

1 view (last 30 days)
Engdaw Chane on 18 Feb 2018
Edited: John BG on 21 Feb 2018
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

John D'Errico on 18 Feb 2018
Edited: John D'Errico on 19 Feb 2018
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.
Engdaw Chane on 19 Feb 2018
Thank you all. @John D'Errico and @Jan Simon thank you for the commitment you are showing.

John BG on 18 Feb 2018
Edited: John BG on 21 Feb 2018
Hi again Engdaw Chane
Mr D'Errico thanks for the correction, but I insist on the point that there is no need for any reshape, at all, neither on the data nor on the indices, look:
A=randi([-10 10],12,14,360); % data
L=repmat([4:9],30,1).*repmat([1:30]',1,numel([4:9])); % selection indices
% or with Mr D'Errico correction that I must say I find it elegant
% L=[4:9]'+[1:12:348];
B=A(:,:,L(:)); % result
Now B is the expected shape.
thanks in advance for time and attention
John BG
John D'Errico on 19 Feb 2018
Edited: John D'Errico on 19 Feb 2018
This does not work. The result is the correct size and shape, but the answer is flat out wrong.
[4:9]'*[1:30]
ans =
Columns 1 through 28
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 156 162 168
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196
8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176 184 192 200 208 216 224
9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198 207 216 225 234 243 252
Columns 29 through 30
116 120
145 150
174 180
203 210
232 240
261 270
If you look in the first column, you will see it generates months 4-9.
In the second year, you want months
12 + (4:9)
ans =
16 17 18 19 20 21
NOT months [8 10 12 14 16 18].
So you will fail to extract the correct months. Right shape, but pure garbage for a result. You should see that in fact, some months are selected several times. Two of those months were actually selected and used 4 times. (Months 72 and 120, in case you care.)
Note that this WOULD have been correct with a different outer product for L. Had John BG written it as
L=[4:9]' + [0:12:348];
Thus, in a current MATLAB release, MATLAB will interpret this as a generalized outer product, creating what is essentially an addition table.
L =
Columns 1 through 28
4 16 28 40 52 64 76 88 100 112 124 136 148 160 172 184 196 208 220 232 244 256 268 280 292 304 316 328
5 17 29 41 53 65 77 89 101 113 125 137 149 161 173 185 197 209 221 233 245 257 269 281 293 305 317 329
6 18 30 42 54 66 78 90 102 114 126 138 150 162 174 186 198 210 222 234 246 258 270 282 294 306 318 330
7 19 31 43 55 67 79 91 103 115 127 139 151 163 175 187 199 211 223 235 247 259 271 283 295 307 319 331
8 20 32 44 56 68 80 92 104 116 128 140 152 164 176 188 200 212 224 236 248 260 272 284 296 308 320 332
9 21 33 45 57 69 81 93 105 117 129 141 153 165 177 189 201 213 225 237 249 261 273 285 297 309 321 333
Columns 29 through 30
340 352
341 353
342 354
343 355
344 356
345 357
You can see that modified as I showed it here, John's approach will extract the correct months, thus [4:9, 16:21, ... ]. What John BG did in posthaste to get a fast answer in, was carelessly assume because the shape of L was correct, that it did what he wanted it to do.