From: magda magdaleno on
There must be an easy way to do this...
I need to shift and rotate a matrix

i.e.
if I start with matrix ...

1 2 3
4 5 6
7 8 9

i'd like to specify how many elements to shift by.. i.e.
shift by 2

8 9 1
2 3 4
5 6 7

i tried circshift but now what i want.
Is there a command that will do this?

Txs in advance
-dave
From: James Tursa on
"magda magdaleno" <d.a.magdaleno(a)gmail.com> wrote in message <hurr3i$3jq$1(a)fred.mathworks.com>...
>
> if I start with matrix ...
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> i'd like to specify how many elements to shift by.. i.e.
> shift by 2
>
> 8 9 1
> 2 3 4
> 5 6 7

>> A = [1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> B = A'
B =
1 4 7
2 5 8
3 6 9
>> B = reshape(circshift(B(:),2),size(A))'
B =
8 9 1
2 3 4
5 6 7

James Tursa
From: Walter Roberson on
magda magdaleno wrote:
> There must be an easy way to do this...
> I need to shift and rotate a matrix
>
> i.e. if I start with matrix ...
>
> 1 2 3 4 5 6 7 8 9
>
> i'd like to specify how many elements to shift by.. i.e. shift by 2
>
> 8 9 1
> 2 3 4
> 5 6 7

> i tried circshift but now what i want. Is there a command that will do
> this?

A = 1:9;
NewDim2 = 3;
NewDim1 = 3;
%note Dim2 and Dim1 are reversed from usual. The reshape goes along
%columns, but you need a reshape along rows, so reshape along columns
%and then transpose
reshape( circshift(A, [0 2]), NewDim2, NewDim1 ) .'
From: Jos (10584) on
"magda magdaleno" <d.a.magdaleno(a)gmail.com> wrote in message <hurr3i$3jq$1(a)fred.mathworks.com>...
> There must be an easy way to do this...
> I need to shift and rotate a matrix
>
> i.e.
> if I start with matrix ...
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> i'd like to specify how many elements to shift by.. i.e.
> shift by 2
>
> 8 9 1
> 2 3 4
> 5 6 7
>
> i tried circshift but now what i want.
> Is there a command that will do this?
>
> Txs in advance
> -dave

There is not a single command to accomplish this, but you can use the power of indexing:

% data
A = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12]
nshift = 3 % even works for negative shifts !
% engine
nA = numel(A)
B = A.'
ix = mod((0:nA-1) + nshift, nA) + 1
B(ix) = B
B = B.'

Note that if you specified the shift to occur in the other dimensions, you could have done without the transpose operations.

hth
Jos
From: magda magdaleno on
guys this works for me!!

Thanks everyone for the help.

-dave