title: In armadillo, how to interpret result of find on submatrix
body: <p>Consider the following code using Armadillo to find the first 7 (searching down columns first) in the specified rows and columns of a matrix:</p>
  
  <pre><code>using namespace arma;
  
  uvec2 firstEqual7(const mat&amp; X, const uvec&amp; rows, const uvec&amp; cols)
  {
      const uword findCount = 1;
      uvec results = find(X.submat(rows, cols) == 7, findCount);
      if (results.empty()) throw std::logic_error("no 7 found!");
      return ind2sub(X, results[0]);
  }
  </code></pre>
  
  <p>There are two problems with this. First, find gives us a single vectorised index for each result, so to convert this to a row and column we have to call a function somewhat like MATLAB's <code>ind2sub</code>, but surprisingly none exists in Armadillo. OK, that's a pain, but I could write one myself in two lines. (But I mention it in case someone knows a better way for this.)</p>
  
  <p>A bigger problem is that the result is an index <em>into the submatrix</em> e.g. if <code>X</code> is 100x100 but <code>rows=cols={98,99}</code> then the bottom right hand corner is indicated by <code>find(...)==3</code> rather than <code>find(...)==9999</code>. How could I get the absolute result? </p>
  
  <p>One option is this:</p>
  
  <pre><code>using namespace arma;
  
  uvec2 firstEqual7(const mat&amp; X, const uvec&amp; rows, const uvec&amp; cols)
  {
      const uword findCount = 1;
      auto submatOfX = X.submat(rows, cols);  // DO NOT DO THIS!
      uvec results = find(submatOfX == 7, findCount);
      if (results.empty()) throw std::logic_error("no 7 found!");
      uvec2 submatIndices = ind2sub(submatOfX, results[0]);
      return uvec2{rows[submatIndices[0]], cols[submatIndices[1]]}; 
  }
  </code></pre>
  
  <p>I think this will run (assuming we've implemented <code>ind2sub</code>), but is wildly inefficient. The problem involves the fact that that <code>submatOfX</code> is not really a matrix but a little proxy type, so that Armadillo doesn't need to copy all the submatrix elements into a new matrix just to perform the find operation. But passing it to <code>ind2sub</code> implicitly converts it to a <code>mat</code>, which makes the matrix copy take place after all!</p>
  
  <p>Pushing on with this possible solution, I could make <code>ind2sub</code> templated on the matrix-like type so that the proxy is passed in directly. This would probably work but seems rather complicated solution to what I had originally hoped would be a very short function. What's more, the proxy types in Armadillo aren't really documented and I don't think that users are meant to mess with them, so I'm worried that keeping one around in a variable, even briefly, is a bit fragile. Does anyone have any better ideas?</p>