From: Matt J on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hq27lc$173$1(a)fred.mathworks.com>...

> > You are correct. The first row should have been shifted by one, my mistake.. I even described that! Thank you for your reply, that cut my run time in 10.
> >
>
> Joe, if you care about speed, this code is faster (but significantly harder to read)
=============

I'm seeing much more speed from the for-loop based implementation below.



% Data

A=rand(1000);
X=1:1000;

tic;
V1=Bruno(A,X);
toc
%Elapsed time is 0.139823 seconds.


tic;
V2=Matt(A,X);
toc;
%Elapsed time is 0.024758 seconds.

isequal(V1,V2) %=1




function B=Bruno(A,X)


% Engine
[m n] = size(A);
ind = bsxfun(@plus,m*mod(bsxfun(@plus,1:n,X(:)-1),n),(1:m)');
B = zeros(size(A));
B(:) = A(ind);


function B=Matt(A,X)


B=A.';

[m n] = size(B);

col=B(:,1);

for ii=1:n

s=X(ii);
col(:)=B(:,ii);

B(1:end-s,ii)=col(s+1:end);
B(end+1-s:end,ii)=col(1:s);

end


B=B.';