title: Find the minimum absolute values along the third dimension in a 3D matrix and ensuring the sign is maintained
body: <p>I have a <code>m X n X k</code> matrix and I want to find the elements that have minimal absolute value along the third dimension for each unique 2D spatial coordinate.  An additional constraint is that once I find these minimum values, the sign of these values (i.e. before I took the absolute value) must be maintained.</p>
  
  <p>The code I wrote to accomplish this is shown below.</p>
  
  <pre><code>tmp = abs(dist); %the size(dist)=[m,n,k]
  [v,ind] = min(tmp,[],3); %find the index of minimal absolute value in the 3rd dimension
  ind = reshape(ind,m*n,1);
  [col,row]=meshgrid(1:n,1:m); row = reshape(row,m*n,1); col = reshape(col,m*n,1);     
  ind2 = sub2ind(size(dist),row,col,ind); % row, col, ind are sub
  dm = dist(ind2); %take the signed value from dist
  dm = reshape(dm,m,n);
  </code></pre>
  
  <p>The resulting matrix <code>dm</code> which is <code>m X n</code> corresponds to the matrix that is subject to the constraints that I have mentioned previously. However, this code sounds a little bit inefficient since I have to generate linear indices. Is there any way to improve this?</p>