title: Get indices of K smallest or largest elements in each row of a matrix in R
body: <p>How to get indices of K smallest or largest elements in eaach row of a matrix in R?</p>
  
  <p>E.g. I have matrix:</p>
  
  <pre><code>2   3   1  65  2
  46  7   9  3   2
  9   45  3  5   7
  24  65  87 3   6
  34  76  54 33  6
  </code></pre>
  
  <p>I'd like to get Indices matrix of say 2 smallest elements (breaking ties in any way) in each row. the result should be in following format:</p>
  
  <pre><code>3 1
  5 4
  3 4
  4 5
  5 4
  </code></pre>
  
  <p>I tried few commands using <code>sort</code>, <code>apply</code>, <code>arrayInd</code>, <code>which</code> etc. But still unable to get desired result.
  Any help is welcome.</p>