title: Accessing values using subscripts without using sub2ind
body: <p>Consider a matrix <strong>M</strong> and a set of subscripts stored in columns <strong>I</strong> and <strong>J</strong>. I need to access the elements designated by I &amp; J without converting them to linear indices (i.e. using <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/sub2ind.html" rel="nofollow noreferrer">sub2ind</a>). E.g.</p>
  
  <pre><code>M = [1 2 3;4 5 6;7 8 9];
  I = [1 1 1];
  J = [1 2 3];
  
  VALS = [1 2 3];
  </code></pre>
  
  <p>Also, doing the following is not feasible since I &amp; J are <a href="https://stackoverflow.com/questions/1144929/learning-decision-trees-on-huge-datasets">huge</a> :</p>
  
  <pre><code>VALS = diag(M(I,J));
  </code></pre>
  
  <p>And for demonstration, this is <strong>not</strong> what I'm looking for,</p>
  
  <pre><code>VALS = M(sub2ind(size(M),I,J));
  </code></pre>
  
  <p>Essentially sub2ind seems to be taking a lot of time and right now I'm looking for methods to access these elements without converting the subscripts to indices. Any other way is feasible as long as it's faster than the method using sub2ind.</p>