title: matlab: addressing of one index without sub2ind
body: <p>This is very closely related to <a href="https://stackoverflow.com/questions/1146719/accessing-values-using-subscripts-without-using-sub2ind">this other question</a>, but that question wanted to avoid <code>sub2ind</code> because of performance concerns. I am more concerned about the "unelegance" of using <code>sub2ind</code>.</p>
  
  <p>Let's suppose I want to create another MxN matrix which is all zeros except for one entry in each column that I want to assign from the corresponding entry in a vector, and choice of row in each column is based on another vector. For example:</p>
  
  <pre><code>z = zeros(10,4);
  rchoice = [3 1 8 7];
  newvals = [123 456 789 10];
  % ??? I would like to set z(3,1)=123, z(1,2)=456, z(8,3)=789, z(7,4)=10
  </code></pre>
  
  <p>I can use <code>sub2ind</code> to accomplish this (which I used in <a href="https://stackoverflow.com/questions/5882827/matlab-vectorize/5884015#5884015">an answer to a closely related question</a>):</p>
  
  <pre><code>z(sub2ind(size(z),rchoice,1:4)) = newvals
  </code></pre>
  
  <p>but is there another alternative? Seems like logical addressing could be used in some way but I'm stumped, because in order to set the elements of a logical matrix to 1, you're dealing with the same element positions as in the matrix you actually want to address.</p>