Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Edit</h2> <p>Pursuit is right: Using a for-loop and appropriate indexing seems to be the way to go here. Here's one way of doing it:</p> <pre><code>[m, n] = size(A); D = randi([0, m - 1], [1, n]); B = zeros(m, n); for i = (1 : n) B(:, i) = [A((m - D(i) + 1 : m), i); A((1 : m - D(i) ), i)]; end </code></pre> <h2>Original answer</h2> <p>I've looked for something similar before, but I never came across a good solution. A modification of one of the algorithms used <a href="http://www.mathworks.com/matlabcentral/fileexchange/22858-circulant-matrix/content/circulant.m" rel="nofollow">here</a> gives a slight performance boost in my tests:</p> <pre><code>[m, n] = size(A); mtxLinearIndices ... = bsxfun(@plus, ... mod(bsxfun(@minus, (0 : m - 1)', D), m), ... (1 : m : m * n)); C = A(idxs); </code></pre> <p>Ugly? Definitely. Like I said, it seems to be slightly faster (2--3 times faster for me); but both algorithms are clocking in at under a second for <code>m = 3000</code> and <code>n = 1000</code> (on a rather old computer, too).</p> <p>It might be worth noting that, for me, both algorithms seem to outperform the algorithm provided by Ansari, though his answer is certainly more straightforward. (Ansari's algorithm's output does not agree with the other two algorithms for me; but that could just be a discrepancy in how the shifts are being applied.) In general, <code>arrayfun</code> seems pretty slow when I've tried to use it. Cell arrays also seem slow to me. But my testing might be biased somehow.</p>
 

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