title: Julia sparse matrix
body: <p>I have vector <code>y_vec</code>, How to convert the vector to a matrix of form <code>Y_matrix</code></p>
  
  <pre><code>y_vec = [0; 1; 1; 2; 3; 4]
  
  Y_matrix = [1 0 0 0 0
              0 1 0 0 0
              0 1 0 0 0
              0 0 1 0 0
              0 0 0 1 0
              0 0 0 0 1]
  </code></pre>
  
  <p>So far, I've tried using a for loop. </p>
  
  <pre><code>Y_mat = full(spzeros(length(y_vec), length(unique(y_vec))))
  
  for (i,j) in enumerate(1:length(y_vec))
      Y_mat[i, y_vec[j]+1] = 1
  end
  </code></pre>
  
  <p>But, there seems to be a problem when <code>y_vec</code> is not continuous, say <code>y_vec</code> = [0; 1; 1; 2; 3; 4; <strong>8</strong>], using for loop fails !!! How to get around this issue.</p>
  
  <p>Is there a way to solve the above problem using sparse matrix in Julia.</p>