I googled on how to increase the speed of scanning images and I found circshift() function which speeds up local neighbour processing. this function also shrank the code lines a lot.
so, here is the medium transmission calculation:
function medtransMat = mediumtransmissionMat ( im , gs )
%%%%%% gs must be an odd num
%%% im must be normalize [0-1]
half=floor(gs*gs/2);
%%%%% add padding to im
paddedim=padarray(im,[half,half],'both');
immin =ones(size(paddedim));
imheight = size ( im , 1 ) ;
imwidth = size ( im , 2 ) ;
%% find brightest pixel in the dark channel- red
maxvalred=max(max(im(:,:,1))) ;
indxmaxvalred = find ( im ( : , : , 1 ) == maxvalred ) ;
%% green global background light- scalar
greenglobalBackLight = im ( indxmaxvalred ( 1 ) +imheight*imwidth ) ;
%% blue global background light- scalar
blueglobalBackLight = im ( indxmaxvalred ( 1 ) +2*imheight*imwidth ) ;
globalBackgLight = double ( [maxvalred , greenglobalBackLight , blueglobalBackLight] );
%%%% loop in green and blue channels to find min
for k=2:3
for i=-half:half
for j=-half:half
immin(:,:,k)=min(immin(:,:,k) , circshift(paddedim(:,:,k),[i,j]));
endfor
endfor
endfor
%%%% loop in red channel only due to different equation
for i=-half:half
for j=-half:half
immin(:,:,1)=min( immin(:,:,1) , ones(size(paddedim(:,:,1)))-circshift(paddedim(:,:,1),[i,j]));
endfor
endfor
%%%% normalize 0-1
immin(:,:,1)=immin(:,:,1)/(1-globalBackgLight(1));
immin(:,:,2)=immin(:,:,2)/globalBackgLight(2);
immin(:,:,3)=immin(:,:,3)/globalBackgLight(3);
%%% remove the padding
imminnopad=immin(half+1:end-half,half+1:end-half,:);
%%% find min in 3 channels
medtransMat=min(imminnopad,[],3);
medtransMat=medtransMat/max(medtransMat(:));
medtransMat=1-medtransMat;
end % end of function