# Manipulation of 3D matrix

Assume that we have a 3D array x, and we would like to split it into 2D slices then cut every slice into some small patches and get all patches in single list t. How to do this?

What I have done is to extract a 2D slice and then cut it into patches but how to do this with all slices in one shot. For example:

x = RandomInteger[{1, 10}, {8, 8, 8}]; (* x is an 8x8x8 array *)
y = x[[1, ;;, ;;]];                    (* y is an 8x8 array   *)
z = Partition[y, {2, 2}, {2, 2}];      (* z is a 4x4x2x2 array *)
t = Flatten[z, 1];                     (* t is a 16x2x2 array *)


How to repeat this for all x $$(8\times8\times8)$$ and get some vector t $$(128\times2\times2)$$?

Thanks for help!

• Just a quick pointer: x[[1,;;,;;]] is a bit easier to read as x[[1,All,All]] but actually it is equivalent to x[]. Oct 17 '18 at 1:43
• yep, u r right :) Oct 17 '18 at 6:00

ArrayReshape is constructive here too (together with the somewhat confusing fourth syntax of Flatten):

ArrayReshape[x, {32, 2, 4, 2}];
Flatten[%, {{1, 3}, {2}, {4}}]

• Interesting... Thanks for help Oct 17 '18 at 9:50
• @AlienTsukuba Glad if it indeed helps you. Oct 17 '18 at 9:57

If you define a helper function

helper[list_] := Partition[list, {2, 2}, {2, 2}]


The you can get what you want by using Map on x:

result = Flatten[helper /@ x, 2];
Dimensions@result
(* {128, 2, 2} *)

• Thanks Jason B, it works fine :) Oct 17 '18 at 5:59