I believe I have found one of the fastest ways to differentiate different portions of a large matrix using built in Matlab functions. However, there is a problem to my method, there is a remaining middle value between the side values. Allow me to demonstate with some simple code which makes use of gpuArray. For my case, I need the data on a gpu.
B = gpuArray(rand(N,N,N));
An = diff(B(1:S+1,:,:),1,1);
Ap = diff(B(end-S:end,:,:),1,1);
A1 = B([2:S+1,N-S+1:N],:,:)-B([1:S,N-S:N-1],:,:);
A2 = diff(B([1:S+1,N-S:N],:,:),1,1);
However diff is coded on the backend of Matlab, and would need to be modified for my application. If this is true, how would I go about learning how to code up my own diff function for this special case?