Fastest way to differentiate only the sides of large matricies
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nathan Zechar
el 2 de Oct. de 2021
Comentada: Nathan Zechar
el 4 de Oct. de 2021
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.
N = 300; % Number of points in x,y,z dimensions
S = 10; % Number of points from side of a dimension
for i = 1:20
B = gpuArray(rand(N,N,N)); % Load some random data
An = diff(B(1:S+1,:,:),1,1); % Differentation from left side
Ap = diff(B(end-S:end,:,:),1,1); % Differentation from right side
A1 = B([2:S+1,N-S+1:N],:,:)-B([1:S,N-S:N-1],:,:);
% The above is the finite difference which is applied to
% both sides of the matrix at the same time. This method
% however, is slower than using built in diff
A2 = diff(B([1:S+1,N-S:N],:,:),1,1);
% The above method is the fastest method, however there is one
% additional data point between S+1 and N - S. Deleting this
% point would add extra computational time
end
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?
Thank you
0 comentarios
Respuesta aceptada
Matt J
el 3 de Oct. de 2021
Editada: Matt J
el 3 de Oct. de 2021
This seems to be about as fast, at least on the GTX 1080 Ti.
T=reshape(B([1:S+1,N-S:N],:) ,S+1,2*N^2 );
A3 = reshape( diff(T,1,1), 2*S,N,N);
5 comentarios
Matt J
el 4 de Oct. de 2021
Editada: Matt J
el 4 de Oct. de 2021
No, you don't need to permute.
% along 1st axis
T = reshape(B([1:S+1,N-S:N],:) ,S+1,2,N,N );
A1 = reshape(diff(T,1,1), 2*S,N,N);
% along 2nd axis
T=reshape(B(:,[1:S+1,N-S:N],:),N,S+1,2,N);
A2=reshape( diff(T,1,2) , N,2*S,N);
% along 3rd axis
T=reshape(B(:,:,[1:S+1,N-S:N]),N,N,S+1,2);
A3=reshape( diff(T,1,3) , N,N,2*S);
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!