6
$\begingroup$

I am looking to partition a list up at a certain depth level. Following code should demonstrate what I mean:

test = Table[Random[], {1000}, {32}, {32}];

result = Map[Partition[#, {5, 5}, 1] &, test]; // AbsoluteTiming

(* {0.5200297, Null} *)

This does what I want, but is there a faster way?

Thanks, Julian.

$\endgroup$
  • $\begingroup$ Probably Partition[test, {1, 5, 5}, 1], except it introduces an unneeded extra layer of braces. (Unfortunately, I am not near a machine to check.) $\endgroup$ – J. M. is away Aug 3 '15 at 11:22
  • $\begingroup$ Actually that's twice as slow. $\endgroup$ – Chris Degnen Aug 3 '15 at 11:38
  • $\begingroup$ @Chris it's a bit faster on my machine. 0.52sec vs 0.44sec. $\endgroup$ – Patrick Stevens Aug 29 '15 at 10:22
1
$\begingroup$
test = RandomReal[{}, {5000, 32, 32}];
cf = Compile[{{A, _Real, 2}}, 
   Evaluate@Quiet@Partition[Table[A[[i, j]], {i, 32}, {j, 32}], {5, 5}, 1], 
   RuntimeAttributes -> Listable];

result1 = Partition[#, {5, 5}, 1] & /@ test; // AbsoluteTiming
result2 = cf@test; // AbsoluteTiming
result1 === result2

enter image description here

$\endgroup$
  • $\begingroup$ On my system, 10.1.0 under Windows 7 x64, timings are 0.706 vs 0.137 so your code is 5X faster, and that is without compilation to C. Well done! $\endgroup$ – Mr.Wizard Aug 9 '16 at 12:45

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.