title: Index array columns using vectors in GNU Octave/MATLAB
body: <p>While coding in GNU Octave/MATLAB I came through this simple problem I couldn't figure out by myself: I'm trying to select some elements of a matrix by using some <em>indexes</em> stored in an array. Let me put it clear with an example:</p>
  
  <p>Given:</p>
  
  <pre><code>A = zeros(5, 3)
  
  0   0   0
  0   0   0
  0   0   0
  0   0   0
  0   0   0
  </code></pre>
  
  <p>I would like to select some elements in A matrix row-wise, by using the values in the auxiliary array B as subindices.</p>
  
  <p>Ie. the following B array</p>
  
  <pre><code>B = [ 1 3 2 1 3 ]'
  
  1
  3
  2
  1
  3
  </code></pre>
  
  <p>should be read as:</p>
  
  <pre><code>1 -&gt; index '1' on first row  (element [1, 1])
  3 -&gt; index '3' on second row (element [2, 3])
  2 -&gt; index '2' on third row  (element [3, 2])
  1 -&gt; index '1' on fourth row (element [4, 1])
  3 -&gt; index '3' on fifth row  (element [5, 3])
  </code></pre>
  
  <p>Therefore, if we assign value '1' to the elements selected using the aforementioned criteria, the resulting matrix would be:</p>
  
  <pre><code>1   0   0
  0   0   1
  0   1   0
  1   0   0
  0   0   1
  </code></pre>
  
  <p>I believe this is a simple operation and I'm convinced that there must be a way to achieve the described behaviour without having to loop across the rows in matrix A.</p>
  
  <p>Thank you.</p>
  
  <p>Edit: Rewrite question so that it is (hopefully) less confusing.</p>