This uses the max_px that you already have. The 4th and 5th line just convert max_px to more natural 3D coordinates, so maybe those could be omitted, and the code shortened, if you can get it in that form to begin with.
dcell=mat2cell(d,...);
[outer_dims{1:3}]=size(dcell); outer_dims=[outer_dims{:}],
[inner_dims{1:3}]=size(dcell{1}); inner_dims=[inner_dims{:}],
idx=sub2ind(inner_dims,max_px(:,1),max_px(:,2));
[ii,jj,kk]= ind2sub(inner_dims,idx); %get 3-inner max coodinates
[X,Y,Z]=ndgrid(1:outer_dims(1),1:outer_dims(2), 1:outer_dims(3));
dims6=[inner_dims; outer_dims];
idx6=sub2ind(dims6(:).', ii,X(:),jj,Y(:),kk,Z(:));
[imax,jmax,kmax] = ind2sub(size(d),idx6);