Thanks Taemin, you're right. I wasn't aware that you can just supply a 1D vector as one of the arguments of convn.
%normalized gaussian kernel
a=[.0003 .1065 .7866 .1065 .0003];
%make a test matrix
A = randn(64,64,64,64);
b=reshape(a,[1 1 n]);
b=reshape(a,[1 1 1 n]);
Thank you, Maxime. Sorry for not including n2s - it's just an alias function I made for num2str. I submitted an update which simply replaces n2s with num2str.
As for trimarray, perhaps the way I wrote it is a little weird, but the goal is to trim pixels away from the edges of the result, to replicate the behaviour of convn with shape set to 'valid' or 'same'.
I only used it with a all-1's 3-D kernel on a volume image (150x200x200) and it works fine. The time decreased from 5s down to 1s (although I was using a MEX version of 3d convolution). Good job!
However, the trimarray part seems a little weird and does not work since n2s does not exist. Any help on that would be appreciated.