Edit
The correct pursuit: using a for loop and appropriate indexing seems to be the way for you. Here is one way to do this:
[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
Original answer
I have been looking for something like this before, but I have never come across a good solution. A modification of one of the algorithms used here gives a slight increase in performance in my tests:
[m, n] = size(A); mtxLinearIndices ... = bsxfun(@plus, ... mod(bsxfun(@minus, (0 : m - 1)', D), m), ... (1 : m : m * n)); C = A(idxs);
Nasty? Definitely. As I said, it looks a little faster (2 to 3 times faster for me); but both algorithms work in seconds for m = 3000 and n = 1000 (on a rather old computer too).
It may be worth noting that for me both algorithms are superior to the algorithm provided by Ansari, although its answer is certainly simpler. (The Ansari algorithm output is not consistent with the other two algorithms for me, but it may just be a mismatch in how shifts are applied.) Overall, arrayfun seems pretty slow when I tried to use it. Cells also seem slow to me. But my testing may be biased in some way.
zroth source share