title: Use sub2ind instead of loop for multidimensional arrays. Matlab
body: <p>I need to find a value of element which has maximum absolute value in a given dimension (in my case second dimension). For 2D matrix it is easy</p>
  
  <pre><code>A=normrnd(0,1,60,150);
  [~,idx]=max(abs(A),[],2);
  MAX=A(sub2ind(size(A), 1:size(A,1), idx'));
  </code></pre>
  
  <p>But with three dimensional situation I have a problem using <code>sub2ind()</code> function. <strong>I understand that it is dummy question, but I really could not find a solution</strong> <em>(may be because of Friday)</em></p>
  
  <p>The working code with for loop:</p>
  
  <pre><code>B=normrnd(0,1,60,150,450);
  [~,idx]=max(abs(B),[],2);
  MAX=nan(size(B,1),size(B,3));
  for iDim1=1:size(B,1)
      for iDim3=1:size(B,3)
          [~,idx]=max(abs(B(iDim1,:,iDim3)));
          MAX(iDim1,iDim3)=B(iDim1,idx,iDim3);
      end %iDim3
  end %iDim1
  </code></pre>
  
  <p><strong>Could you provide <code>sub2ind()</code> or another elegant solution for this loop?</strong></p>