MATLAB: Reshape a 4D array to a 2D matrix where the size of the arrays vary

4d arrayMATLABmatrixreshape

I have a 4D array
MM = [ 2.*N+1 , 2.*N+1 , Cylinder , Cylinder ] ;
where N is a scale for a contour plot and Cylinder defines the number of 2N+1 x 2N+1 arrays; so for example, N = 100 and Cylinder = 3 would produce an array of nine 201 x 201 square matrices. I want to reshape MM into a square 2D matrix, so the output always has a consistent format as the values of N and Cylinder vary.
For example, Cylinder = 2 would give MM the structure
val(:,:,1,1) = ...
val(:,:,2,1) = ...
val(:,:,1,2) = ...
val(:,:,2,2) = ...
I want to reshape it as
[ val(:,:,1,1) , val(:,:,2,1) ; ...
val(:,:,1,2) , val(:,:,2,2) ] ;
So for Cylinder = 3, I want
[ val(:,:,1,1) , val(:,:,2,1) , val(:,:,3,1) ; ...
val(:,:,1,2) , val(:,:,2,2) , val(:,:,3,2) ; ...
val(:,:,1,3) , val(:,:,2,3) , val(:,:,3,3) ] ;
and Cylinder = 4,
[ val(:,:,1,1) , val(:,:,2,1) , val(:,:,3,1) , val(:,:,4,1) ; ...
val(:,:,1,2) , val(:,:,2,2) , val(:,:,3,2) , val(:,:,4,2) ; ...
val(:,:,1,3) , val(:,:,2,3) , val(:,:,3,3) , val(:,:,4,3) ; ...
val(:,:,1,4) , val(:,:,2,4) , val(:,:,3,4) , val(:,:,4,4) ] ;
etc. How can this structure be achieved for any value of N and Cylinder?

Best Answer

You can use permute and reshape:
S = size(M);
Z = reshape(permute(M,[1,4,2,3]),cyl*S(1:2))
Here is a little test code for you to try:
% fake data:
N = 2;
cyl = 3;
M = zeros(2*N+1,2*N+1,cyl,cyl);
M(:) = 1:numel(M)
% reshape:
S = size(M);
Z = reshape(permute(M,[1,4,2,3]),cyl*S(1:2))
And you can see the input M and output Z:
M(:,:,1,1) =
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
M(:,:,2,1) =
26 31 36 41 46
27 32 37 42 47
28 33 38 43 48
29 34 39 44 49
30 35 40 45 50
M(:,:,3,1) =
51 56 61 66 71
52 57 62 67 72
53 58 63 68 73
54 59 64 69 74
55 60 65 70 75
M(:,:,1,2) =
76 81 86 91 96
77 82 87 92 97
78 83 88 93 98
79 84 89 94 99
80 85 90 95 100
M(:,:,2,2) =
101 106 111 116 121
102 107 112 117 122
103 108 113 118 123
104 109 114 119 124
105 110 115 120 125
M(:,:,3,2) =
126 131 136 141 146
127 132 137 142 147
128 133 138 143 148
129 134 139 144 149
130 135 140 145 150
M(:,:,1,3) =
151 156 161 166 171
152 157 162 167 172
153 158 163 168 173
154 159 164 169 174
155 160 165 170 175
M(:,:,2,3) =
176 181 186 191 196
177 182 187 192 197
178 183 188 193 198
179 184 189 194 199
180 185 190 195 200
M(:,:,3,3) =
201 206 211 216 221
202 207 212 217 222
203 208 213 218 223
204 209 214 219 224
205 210 215 220 225
Z =
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71
2 7 12 17 22 27 32 37 42 47 52 57 62 67 72
3 8 13 18 23 28 33 38 43 48 53 58 63 68 73
4 9 14 19 24 29 34 39 44 49 54 59 64 69 74
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
76 81 86 91 96 101 106 111 116 121 126 131 136 141 146
77 82 87 92 97 102 107 112 117 122 127 132 137 142 147
78 83 88 93 98 103 108 113 118 123 128 133 138 143 148
79 84 89 94 99 104 109 114 119 124 129 134 139 144 149
80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
151 156 161 166 171 176 181 186 191 196 201 206 211 216 221
152 157 162 167 172 177 182 187 192 197 202 207 212 217 222
153 158 163 168 173 178 183 188 193 198 203 208 213 218 223
154 159 164 169 174 179 184 189 194 199 204 209 214 219 224
155 160 165 170 175 180 185 190 195 200 205 210 215 220 225