3
$\begingroup$

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!

$\endgroup$
2
  • 4
    $\begingroup$ Just a quick pointer: x[[1,;;,;;]] is a bit easier to read as x[[1,All,All]] but actually it is equivalent to x[[1]]. $\endgroup$
    – Jason B.
    Oct 17 '18 at 1:43
  • $\begingroup$ yep, u r right :) $\endgroup$ Oct 17 '18 at 6:00
3
$\begingroup$

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}}]
$\endgroup$
2
  • $\begingroup$ Interesting... Thanks for help $\endgroup$ Oct 17 '18 at 9:50
  • $\begingroup$ @AlienTsukuba Glad if it indeed helps you. $\endgroup$ Oct 17 '18 at 9:57
1
$\begingroup$

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} *)
$\endgroup$
1
  • $\begingroup$ Thanks Jason B, it works fine :) $\endgroup$ Oct 17 '18 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.