Programming Previous page   Next Page

Shifting the Location of Matrix Elements

The circshift function shifts the elements of a matrix in a circular manner along one or more dimensions. Rows or columns that are shifted out of the matrix circulate back into the opposite end. For example, shifting a 4-by-7 matrix one place to the left moves the elements in columns 2 through 7 to columns 1 through 6, and moves column 1 to column 7.

Create a 5-by-8 matrix named A and shift it to the right along the second (horizontal) dimension by three places. (You would use [0, -3] to shift to the left by three places):

Now take A and shift it along both dimensions: three columns to the right and two rows up:

Since circshift circulates shifted rows and columns around to the other end of a matrix, shifting by the exact size of A returns all rows and columns to their original location:


Previous page  Shifting and Sorting Matrices Sorting the Data in Each Column Next page

© 1994-2005 The MathWorks, Inc.