What I forgot to mention here: wouldn't it make sense to initialize H as a 3D array in the first place and skip the out array entirely here?