title: Why this Octave code won't work?
body: <p>Let <code>Y</code> be a vector of length <code>N</code>, containing numbers from <code>1</code> to <code>10</code>. As example code you can use:</p>
  
  
  
  <pre class="lang-matlab prettyprint-override"><code>Y = vec(1:10);
  </code></pre>
  
  <p>I am writing the code which must create an <code>N x 10</code> matrix, each row consisting of all zeros except for a <code>1</code> only in the position which corresponds to the number in vector <code>Y</code>. Thus, <code>1</code> in <code>Y</code> becomes <code>10000000000</code>, <code>3</code> becomes <code>0010000000</code>, and so on.</p>
  
  <p>This approach works:</p>
  
  <pre class="lang-matlab prettyprint-override"><code>cell2mat(arrayfun(@(x)eye(10)(x,:), Y, 'UniformOutput', false))
  </code></pre>
  
  <p>My next idea was to "optimize", so <code>eye(10)</code> is not generated <code>N</code> times, and I wrote this:</p>
  
  <pre class="lang-matlab prettyprint-override"><code>theEye = eye(10);
  cell2mat(arrayfun(@(x)theEye(x,:), Y, 'UniformOutput', false))
  </code></pre>
  
  <p>However, now Octave is giving me error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: can't perform indexing operations for diagonal matrix type
  error: evaluating argument list element number 1
  </code></pre>
  
  <p><strong>Why do I get this error? What is wrong?</strong></p>
  
  <p>Bonus questions — do you see a better way to do what I am doing? Is my attempt to optimize making things easier for Octave?</p>