3

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!

2 Answers2

3

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}}]
1

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} *)
Jason B.
  • 68,381
  • 3
  • 139
  • 286