From: Joe McGlinchy on
Hi All!

This seems pretty simple, but I am having trouble with it. lets say I have a matrix A

A= [1 1 2 0
1 1 3 0
1 1 4 0]
that I want to shift horizontally, row wise, where the shift values are in a vector X

X= [1
2
3]

so that when I would do something like circshift(A,X) I want to get out

[ 1 1 2 0
3 0 1 1
0 1 1 4]

It seems like matlab's circshift function doesn't support this, And I would like to avoid looping through to accomplish this if at all possible. Thanks for any ideas!

Joe
From: Bruno Luong on
"Joe McGlinchy" <jxm8863(a)rit.edu> wrote in message <hq2253$7ko$1(a)fred.mathworks.com>...
> Hi All!
>
> This seems pretty simple, but I am having trouble with it. lets say I have a matrix A
>
> A= [1 1 2 0
> 1 1 3 0
> 1 1 4 0]
> that I want to shift horizontally, row wise, where the shift values are in a vector X
>
> X= [1
> 2
> 3]
>
> so that when I would do something like circshift(A,X) I want to get out
>
> [ 1 1 2 0
> 3 0 1 1
> 0 1 1 4]
>
>

Are you sure the first row of the output is unchanged? I though it should be shifted by 1. Here is one among many ways:

% Data
A=[1 1 2 0; 1 1 3 0; 1 1 4 0]
X=[1; 2; 3]

% Engine
[m n] = size(A);
[R C]=ndgrid(1:m,1:n);
C=mod(bsxfun(@plus,C,X(:)-1),n)+1;
B = A;
B(:) = A(sub2ind([m n], R, C))

% Bruno
From: Joe McGlinchy on
Bruno,

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.

Cheers,
Joe
From: Matt J on
"Joe McGlinchy" <jxm8863(a)rit.edu> wrote in message <hq2253$7ko$1(a)fred.mathworks.com>...

> It seems like matlab's circshift function doesn't support this, And I would like to avoid looping through to accomplish this if at all possible. Thanks for any ideas!
==========

I think you're best off looping.
From: Bruno Luong on
"Joe McGlinchy" <jxm8863(a)rit.edu> wrote in message <hq25c6$msq$1(a)fred.mathworks.com>...
> Bruno,
>
> 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)

[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);

% Bruno