Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The documentation in <a href="http://www.mathworks.co.uk/help/matlab/ref/for.html" rel="nofollow"><code>for()</code></a> states:</p> <blockquote> <pre><code>for index = values ... end </code></pre> <p>where <code>values</code> has one of the following forms:</p> <ul> <li><p>...</p></li> <li><p><code>valArray</code>: creates a column vector index from subsequent columns of array <code>valArray</code> on each iteration. For example, on the first iteration, <code>index = valArray(:,1)</code>. The loop executes for a maximum of <code>n</code> times, where <code>n</code> is the number of columns of valArray, given by <code>numel(valArray, 1, :)</code>. The input <code>valArray</code> can be of any MATLAB data type, including a string, cell array, or struct.</p></li> </ul> </blockquote> <p>Therefore, I assume there is a significant <strong>overhead</strong> and the compiler does not check whether <code>1:ksize == klist</code> to exploit the faster implementation. In other words, per Eitan's comment, the JIT applies to the first two types of accepted <em>values</em>.</p> <p>The whole problem is related to the following indexing task (column vs element): </p> <pre><code>tic for m = 1:100000 for k = 1:ksize klist(:,k); end end toc tic for m = 1:100000 for k = 1:ksize klist(k); end end toc Index column: ~2.9 sec Index element: ~0.28 sec </code></pre> <p>You can see how <code>klist(:,k)</code> effectively slows down the faster loop indicating that the issue in <code>for k = klist</code> is related to the column indexing used in this case.</p> <p>For additional details see this <a href="http://www.mathworks.co.uk/matlabcentral/answers/54522-why-is-indexing-vectors-matrices-in-matlab-very-inefficient" rel="nofollow">lengthy discussion on (inefficient) indexing</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload