/2;
    T = T - reshape(M(i)*dMds(j,:,:) + M(j)*dMds(i,:,:),D,D);
    dSds(i,j,:,:) = T; dSds(j,i,:,:) = T;

    <span class="keyword">if</span> i==j
      dSdt(i,i,:,i) = (beta(:,i)'*(L+L'))/(K2)*t <span class="keyword">...</span>
        - 2*dMdt(i,:,i)*M(i);
      dSdX(i,j,:,i) = reshape(dSdX(i,j,:,i),1,D+2) - M(i)*dMdX(j,:,j)-M(j)*dMdX(i,:,i);
    <span class="keyword">else</span>
      dSdt(i,j,:,i) = (beta(:,j)'*L')/(K2)*t <span class="keyword">...</span>
        - dMdt(i,:,i)*M(j);
      dSdt(i,j,:,j