title: Matlab, Integer vector to binary matrix without loop
body: <p>I have a vector with N elements, all integers 1-M. I want to convert this to a NxM matrix with each row containing only zeros except for the i:th element set to one, i being the integer in the vector.</p>
  
  <p>For example:
  [1 1 3] => [1 0 0; 1 0 0; 0 0 1]</p>
  
  <p>I currently do this in a loop, like this:</p>
  
  <pre><code>y_vec = zeros(m, num_labels);
  for i = 1:m
      y_vec(i, y(i)) = 1;
  end
  </code></pre>
  
  <p>Is there a  way to do this without a loop?</p>