Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
shifting row in large matrix

Subject: shifting row in large matrix

From: mahmud

Date: 25 Feb, 2009 07:50:42

Message: 1 of 14

does anyone know how to shift element in a row?

example:

1 2 3 4 5
1 1 1 0 0
5 4 3 2 1

becomes

1 2 3 4 5
0 1 1 1 0
5 4 3 2 1

anyone? or you can show me if there is a function in matlab that can do that.
tq

Subject: shifting row in large matrix

From: Roger Stafford

Date: 25 Feb, 2009 08:29:01

Message: 2 of 14

mahmud <mahmusyubli@gmail.com> wrote in message <18203389.1235548336327.JavaMail.jakarta@nitrogen.mathforum.org>...
> does anyone know how to shift element in a row?
>
> example:
>
> 1 2 3 4 5
> 1 1 1 0 0
> 5 4 3 2 1
>
> becomes
>
> 1 2 3 4 5
> 0 1 1 1 0
> 5 4 3 2 1
>
> anyone? or you can show me if there is a function in matlab that can do that.
> tq

  To shift a row r to the right by p positions do this

 n = length(r);
 r(mod(1:n+p-1,n)+1) = r;

Roger Stafford

Subject: shifting row in large matrix

From: Roger Stafford

Date: 25 Feb, 2009 08:33:01

Message: 3 of 14

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <go2vgd$sg0$1@fred.mathworks.com>...
> mahmud <mahmusyubli@gmail.com> wrote in message <18203389.1235548336327.JavaMail.jakarta@nitrogen.mathforum.org>...
> > does anyone know how to shift element in a row?
> >
> > example:
> >
> > 1 2 3 4 5
> > 1 1 1 0 0
> > 5 4 3 2 1
> >
> > becomes
> >
> > 1 2 3 4 5
> > 0 1 1 1 0
> > 5 4 3 2 1
> >
> > anyone? or you can show me if there is a function in matlab that can do that.
> > tq
>
> To shift a row r to the right by p positions do this
>
> n = length(r);
> r(mod(1:n+p-1,n)+1) = r;
>
> Roger Stafford

  Actually it is easier to use 'circshift'.

Roger Stafford

Subject: shifting row in large matrix

From: mahmud

Date: 25 Feb, 2009 09:31:09

Message: 4 of 14

tq very much for the tip, but i just want to move only one row, the second row (in the middle). for this, using circshift seems difficult make it. any more tips?

Subject: shifting row in large matrix

From: Nasser Abbasi

Date: 25 Feb, 2009 10:20:54

Message: 5 of 14


"mahmud" <mahmusyubli@gmail.com> wrote in message
news:26273456.1235554299812.JavaMail.jakarta@nitrogen.mathforum.org...
> tq very much for the tip, but i just want to move only one row, the second
> row (in the middle). for this, using circshift seems difficult make it.
> any more tips?

EDU>> A=[ 1 2 3 4 5;
                     1 1 1 0 0;
                     5 4 3 2 1];

A(2,:)=circshift(A(2,:),[1 1]);

A


     1 2 3 4 5
     0 1 1 1 0
     5 4 3 2 1


--Nasser

Subject: shifting row in large matrix

From: Jos

Date: 25 Feb, 2009 10:21:02

Message: 6 of 14

mahmud <mahmusyubli@gmail.com> wrote in message <26273456.1235554299812.JavaMail.jakarta@nitrogen.mathforum.org>...
> tq very much for the tip, but i just want to move only one row, the second row (in the middle). for this, using circshift seems difficult make it. any more tips?

What about

A = 10* repmat(1:8,3,1) ;
A(2,:) = A(2,[2:end 1]) ;

Jos

Subject: shifting row in large matrix

From: mahmud

Date: 26 Feb, 2009 05:56:22

Message: 7 of 14

again, tq very much, those are very helpful, but if i have

1 2 3 4 5 6 7 8 9 0
0 1 1 1 1 1 0 0 0 0
0 1 2 3 4 5 6 7 8 9

becomes

1 2 3 4 5 6 7 8 9 0
0 1 1 0 1 1 1 0 0 0
0 1 2 3 4 5 6 7 8 9
 then

1 2 3 4 5 6 7 8 9 0
0 1 1 0 0 1 1 1 0 0
0 1 2 3 4 5 6 7 8 9

partial of row 2 i want it moves, can you guys give me more MATLAB trick about this?

Subject: shifting row in large matrix

From: Nasser Abbasi

Date: 26 Feb, 2009 08:18:42

Message: 8 of 14


"mahmud" <mahmusyubli@gmail.com> wrote in message
news:20631464.1235627813488.JavaMail.jakarta@nitrogen.mathforum.org...
> again, tq very much, those are very helpful, but if i have
>
> 1 2 3 4 5 6 7 8 9 0
> 0 1 1 1 1 1 0 0 0 0
> 0 1 2 3 4 5 6 7 8 9
>
> becomes
>
> 1 2 3 4 5 6 7 8 9 0
> 0 1 1 0 1 1 1 0 0 0
> 0 1 2 3 4 5 6 7 8 9
> then
>
> 1 2 3 4 5 6 7 8 9 0
> 0 1 1 0 0 1 1 1 0 0
> 0 1 2 3 4 5 6 7 8 9
>
> partial of row 2 i want it moves, can you guys give me more MATLAB trick
> about this?


A=[ 1 2 3 4 5 6 7 8 9 0;
       0 1 1 1 1 1 0 0 0 0;
       0 1 2 3 4 5 6 7 8 9]

A(2,4:end)=circshift(A(2,4:end),[1 1])
A(2,4:end)=circshift(A(2,4:end),[1 1])

--- output

A =

     1 2 3 4 5 6 7 8 9 0
     0 1 1 1 1 1 0 0 0 0
     0 1 2 3 4 5 6 7 8 9


A =

     1 2 3 4 5 6 7 8 9 0
     0 1 1 0 1 1 1 0 0 0
     0 1 2 3 4 5 6 7 8 9


A =

     1 2 3 4 5 6 7 8 9 0
     0 1 1 0 0 1 1 1 0 0
     0 1 2 3 4 5 6 7 8 9

EDU>>

--Nasser

Subject: shifting row in large matrix

From: teresa.sabramowicz@gmail.com

Date: 17 Mar, 2009 00:44:16

Message: 9 of 14

On Feb 26, 4:18=A0am, "Nasser Abbasi" <n...@12000.org> wrote:
> "mahmud" <mahmusyu...@gmail.com> wrote in message
>
> news:20631464.1235627813488.JavaMail.jakarta@nitrogen.mathforum.org...
>
>
>
>
>
> > again, tq very much, those are very helpful, but if i have
>
> > 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7 =A0 =A0 8=
 =A0 =A0 9 =A0 =A0 0
> > 0 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0 =A0 =A0 0=
 =A0 =A0 0 =A0 =A0 0
> > 0 =A0 =A0 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7=
 =A0 =A0 8 =A0 =A0 9
>
> > becomes
>
> > 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7 =A0 =A0 8=
 =A0 =A0 9 =A0 =A0 0
> > 0 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0=
 =A0 =A0 0 =A0 =A0 0
> > 0 =A0 =A0 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7=
 =A0 =A0 8 =A0 =A0 9
> > then
>
> > 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7 =A0 =A0 8=
 =A0 =A0 9 =A0 =A0 0
> > 0 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0 =A0 =A0 0 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1=
 =A0 =A0 0 =A0 =A0 0
> > 0 =A0 =A0 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7=
 =A0 =A0 8 =A0 =A0 9
>
> > partial of row 2 i want it moves, can you guys give me more MATLAB tric=
k
> > about this?
>
> A=3D[ 1 2 3 4 5 6 7 8 9 0;
> =A0 =A0 =A0 =A00 1 1 1 1 1 0 0 0 0;
> =A0 =A0 =A0 =A00 1 2 3 4 5 6 7 8 9]
>
> A(2,4:end)=3Dcircshift(A(2,4:end),[1 1])
> A(2,4:end)=3Dcircshift(A(2,4:end),[1 1])
>
> --- output
>
> A =3D
>
> =A0 =A0 =A01 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7 =
=A0 =A0 8 =A0 =A0 9 =A0 =A0 0
> =A0 =A0 =A00 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0 =
=A0 =A0 0 =A0 =A0 0 =A0 =A0 0
> =A0 =A0 =A00 =A0 =A0 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =
=A0 =A0 7 =A0 =A0 8 =A0 =A0 9
>
> A =3D
>
> =A0 =A0 =A01 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7 =
=A0 =A0 8 =A0 =A0 9 =A0 =A0 0
> =A0 =A0 =A00 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =
=A0 =A0 0 =A0 =A0 0 =A0 =A0 0
> =A0 =A0 =A00 =A0 =A0 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =
=A0 =A0 7 =A0 =A0 8 =A0 =A0 9
>
> A =3D
>
> =A0 =A0 =A01 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =A0 =A0 7 =
=A0 =A0 8 =A0 =A0 9 =A0 =A0 0
> =A0 =A0 =A00 =A0 =A0 1 =A0 =A0 1 =A0 =A0 0 =A0 =A0 0 =A0 =A0 1 =A0 =A0 1 =
=A0 =A0 1 =A0 =A0 0 =A0 =A0 0
> =A0 =A0 =A00 =A0 =A0 1 =A0 =A0 2 =A0 =A0 3 =A0 =A0 4 =A0 =A0 5 =A0 =A0 6 =
=A0 =A0 7 =A0 =A0 8 =A0 =A0 9
>
> EDU>>
>
> --Nasser- Hide quoted text -
>
> - Show quoted text -

I have a question about a related subject

I have an n x m matrix
examples are
[1 2 3 4 5]

[1 2 3 4 5;6 7 8 9;10 11 12 13 14]

[1;2;3;4;5]

how do I get the elements to move to the right around the matrix p
times?

So for instance

if p =3D 1
[5 1 2 3 4]
p=3D2
[4 5 1 2 3]
p=3D3
[3 4 5 1 2]

for larger matrices the same thing
p=3D0
[1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15]
p=3D1
[5 1 2 3 4;10 6 7 8 9; 15 11 12 13 14]

so no matter how many times I want to move it it just keeps shifting
the numbers over one place....
if p =3D100 it would shift each element 100 times

right now I am using B =3D A(:.[n-p+1:n 1:n-p]) but it stops working
because of the indexing

Thanks much

Subject: shifting row in large matrix

From: Bruno Luong

Date: 17 Mar, 2009 06:01:08

Message: 10 of 14

teresa.sabramowicz@gmail.com wrote in message <8a86ed0b-1f36-4b36-a6c8-7f6d8a2c808e@q11g2000yqh.googlegroups.com>...

>
> I have a question about a related subject
>
> I have an n x m matrix
> examples are
> [1 2 3 4 5]
>
> [1 2 3 4 5;6 7 8 9;10 11 12 13 14]
>
> [1;2;3;4;5]
>
> how do I get the elements to move to the right around the matrix p
> times?
>
> So for instance
>
> if p =3D 1
> [5 1 2 3 4]
> p=3D2
> [4 5 1 2 3]
> p=3D3
> [3 4 5 1 2]
>
> for larger matrices the same thing
> p=3D0
> [1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15]
> p=3D1
> [5 1 2 3 4;10 6 7 8 9; 15 11 12 13 14]
>
> so no matter how many times I want to move it it just keeps shifting
> the numbers over one place....
> if p =3D100 it would shift each element 100 times

help CIRCSHIFT

Bruno

Subject: shifting row in large matrix

From: Steve Amphlett

Date: 17 Mar, 2009 09:51:00

Message: 11 of 14

mahmud <mahmusyubli@gmail.com> wrote in message <18203389.1235548336327.JavaMail.jakarta@nitrogen.mathforum.org>...
> does anyone know how to shift element in a row?
>
> example:
>
> 1 2 3 4 5
> 1 1 1 0 0
> 5 4 3 2 1
>
> becomes
>
> 1 2 3 4 5
> 0 1 1 1 0
> 5 4 3 2 1
>
> anyone? or you can show me if there is a function in matlab that can do that.
> tq

How big is big? Too big to be able to afford to copy the whole matrix as part of the operation?

Subject: shifting row in large matrix

From: teresa.sabramowicz@gmail.com

Date: 18 Mar, 2009 17:36:02

Message: 12 of 14

On Mar 17, 5:51=A0am, "Steve Amphlett" <Firstname.Lastn...@Where-I-
Work.com> wrote:
> mahmud <mahmusyu...@gmail.com> wrote in message <18203389.1235548336327.J=
avaMail.jaka...@nitrogen.mathforum.org>...
> > does anyone know how to shift element in a row?
>
> > example:
>
> > 1 2 3 4 5
> > 1 1 1 0 0
> > 5 4 3 2 1
>
> > becomes
>
> > 1 2 3 4 5
> > 0 1 1 1 0
> > 5 4 3 2 1
>
> > anyone? or you can show me if there is a function in matlab that can do=
 that.
> > tq
>
> How big is big? =A0Too big to be able to afford to copy the whole matrix =
as part of the operation?- Hide quoted text -
>
> - Show quoted text -

Thanks for that.

I was looking into cirshift and I am not sure if it will ultimately
help me.

I am designing a program that needs to have its matrix element shifted
p times around the perimeter alone. Clockwise or CCW while leaving
the elements in the center unchanged.

Can circshift accomplish this or is there some kind of indexing I
could use?
what would be the way to accomplish this p times?

so for example
p =3D 0
[1 2 3;8 9 4; 7 6 5]
p =3D 1
[8 1 2; 7 9 3; 6 5 4]
p =3D 2
[7 8 1;6 9 2; 5 4 3]

and so on...


Thank you for looking into this.

Subject: shifting row in large matrix

From: Bruno Luong

Date: 18 Mar, 2009 20:10:18

Message: 13 of 14

teresa.sabramowicz@gmail.com wrote in message <fde43de2-7e0a-4076-9d55-9593dc0c5b8a@v38g2000yqb.googlegroups.com>...

>
> I was looking into cirshift and I am not sure if it will ultimately
> help me.
>
> I am designing a program that needs to have its matrix element shifted
> p times around the perimeter alone. Clockwise or CCW while leaving
> the elements in the center unchanged.
>
> Can circshift accomplish this or is there some kind of indexing I
> could use?
> what would be the way to accomplish this p times?
>
> so for example
> p =3D 0
> [1 2 3;8 9 4; 7 6 5]
> p =3D 1
> [8 1 2; 7 9 3; 6 5 4]
> p =3D 2
> [7 8 1;6 9 2; 5 4 3]
>

Teresa,

Cheese!!! This (circumference shift) is different from the example you gave on post #9 above (row shift), which was a straightforward call of circshift. For the minimum of respect to others (some of us do not have much time), you must know what you really want.

A=ceil(10*rand(5,7))
p=2

% Index of the boundary
[m n]=size(A);
I1 = 1:m-1; J1=ones(size(I1));
J2= 1:n-1; I2=m+zeros(size(J2));
I3 = m:-1:2; J3=n+zeros(size(I3));
J4= n:-1:2; I4=ones(size(J4));

% Convert to linear
I=sub2ind(size(A),...
          [I1 I2 I3 I4], ...
          [J1 J2 J3 J4]);
    
% Shift and assign
A(I)=A(circshift(I,[0 -p]))

%Bruno

Subject: shifting row in large matrix

From: teresa.sabramowicz@gmail.com

Date: 18 Mar, 2009 23:42:49

Message: 14 of 14

On Mar 18, 4:10=A0pm, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> teresa.sabramow...@gmail.com wrote in message <fde43de2-7e0a-4076-9d55-95=
93dc0c5...@v38g2000yqb.googlegroups.com>...
>
> > I was looking into cirshift and I am not sure if it will ultimately
> > help me.
>
> > I am designing a program that needs to have its matrix element shifted
> > p times around the perimeter alone. =A0Clockwise or CCW while leaving
> > the elements in the center unchanged.
>
> > Can circshift accomplish this or is there some kind of indexing I
> > could use?
> > what would be the way to accomplish this p times?
>
> > so for example
> > p =3D3D 0
> > [1 2 3;8 9 4; 7 6 5]
> > p =3D3D 1
> > [8 1 2; 7 9 3; 6 5 4]
> > p =3D3D 2
> > [7 8 1;6 9 =A02; 5 4 3]
>
> Teresa,
>
> Cheese!!! This (circumference shift) is different from the example you ga=
ve on post #9 above (row shift), which was a straightforward call of circsh=
ift. For the minimum of respect to others (some of us do not have much time=
), you must know what you really want.
>
> A=3Dceil(10*rand(5,7))
> p=3D2
>
> % Index of the boundary
> [m n]=3Dsize(A);
> I1 =3D 1:m-1; J1=3Dones(size(I1));
> J2=3D 1:n-1; I2=3Dm+zeros(size(J2));
> I3 =3D m:-1:2; J3=3Dn+zeros(size(I3));
> J4=3D n:-1:2; I4=3Dones(size(J4));
>
> % Convert to linear
> I=3Dsub2ind(size(A),...
> =A0 =A0 =A0 =A0 =A0 [I1 I2 I3 I4], ...
> =A0 =A0 =A0 =A0 =A0 [J1 J2 J3 J4]);
>
> % Shift and assign
> A(I)=3DA(circshift(I,[0 -p]))
>
> %Bruno- Hide quoted text -
>
> - Show quoted text -

Thanks very much. I understand your reasoning but my problem changed
quickly and I had to rethink it.

I appreciate you taking the time but I just didnt understand the
circshift function so that is why I asked it.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us