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:
matrix columns rearangement

Subject: matrix columns rearangement

From: Jehad Zughyer

Date: 28 Dec, 2008 19:59:02

Message: 1 of 4

I am creating matrices out of the first m x n matrix. however with each created matrix, I have to rearrange the columns and shift the first column to the right one column. Reshape and circshift did not do what I intend. I am demonstrating what I need to do using a 3X4 matrix:

a= 0 0 0
     0 0 1
     0 1 0
     0 1 1
I always need to create a big ( A ) matrix such as replicating (a ) the same number of its columns ( 3 in this example). I start with (a), then the next (a) I need to shift the first column (always all zeros) to the second column. Then the third (a), moves it to the third column.... Keep doing this until n , number of columns is reached. The end matrix for this example will look like this:

A= 0 0 0
     0 0 1
     0 1 0
     0 1 1
     0 0 0
     0 0 1
     1 0 0
     1 0 1
     0 0 0
     0 1 0
     1 0 0
     1 1 0

Any tips?

Subject: matrix columns rearangement

From: Nasser Abbasi

Date: 28 Dec, 2008 21:00:24

Message: 2 of 4


"Jehad Zughyer" <zjehad@yahoo.com> wrote in message
news:gj8lq6$h6e$1@fred.mathworks.com...
>I am creating matrices out of the first m x n matrix. however with each
>created matrix, I have to rearrange the columns and shift the first column
>to the right one column. Reshape and circshift did not do what I intend. I
>am demonstrating what I need to do using a 3X4 matrix:
>
> a= 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> I always need to create a big ( A ) matrix such as replicating (a ) the
> same number of its columns ( 3 in this example). I start with (a), then
> the next (a) I need to shift the first column (always all zeros) to the
> second column. Then the third (a), moves it to the third column.... Keep
> doing this until n , number of columns is reached. The end matrix for
> this example will look like this:
>
> A= 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> 0 0 0
> 0 0 1
> 1 0 0
> 1 0 1
> 0 0 0
> 0 1 0
> 1 0 0
> 1 1 0
>
> Any tips?
>

Just use a for loop?

>> a=[ 0 0 0;
            0 0 1;
            0 1 0;
            0 1 1];
[nRow,nCol]=size(a);
A=zeros(nRow*nCol,nCol);
A(1:nRow,:)=a;

for i=1:nCol-1
     x=a(:,i);
    a(:,i)=a(:,i+1);
    a(:,i+1)=x;
    A(i*nRow+1:(i+1)*nRow,:)=a;
end

A =
0 0 0
0 0 1
0 1 0
0 1 1
0 0 0
0 0 1
1 0 0
1 0 1
0 0 0
0 1 0
1 0 0
1 1 0

--Nasser

Subject: matrix columns rearangement

From: Jehad Zughyer

Date: 28 Dec, 2008 23:23:01

Message: 3 of 4

Thanks,
worked perfect.
----------------------
"Jehad Zughyer" <zjehad@yahoo.com> wrote in message <gj8lq6$h6e$1@fred.mathworks.com>...
> I am creating matrices out of the first m x n matrix. however with each created matrix, I have to rearrange the columns and shift the first column to the right one column. Reshape and circshift did not do what I intend. I am demonstrating what I need to do using a 3X4 matrix:
>
> a= 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> I always need to create a big ( A ) matrix such as replicating (a ) the same number of its columns ( 3 in this example). I start with (a), then the next (a) I need to shift the first column (always all zeros) to the second column. Then the third (a), moves it to the third column.... Keep doing this until n , number of columns is reached. The end matrix for this example will look like this:
>
> A= 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> 0 0 0
> 0 0 1
> 1 0 0
> 1 0 1
> 0 0 0
> 0 1 0
> 1 0 0
> 1 1 0
>
> Any tips?

Subject: matrix columns rearangement

From: Roger Stafford

Date: 29 Dec, 2008 00:58:01

Message: 4 of 4

"Jehad Zughyer" <zjehad@yahoo.com> wrote in message <gj8lq6$h6e$1@fred.mathworks.com>...
> I am creating matrices out of the first m x n matrix. however with each ......

  For a solution without a for-loop try this:

 [m,n] = size(a);
 [I,J,K] = ndgrid(1:m,1:n,1:n);
 A = reshape(a(I+m*(K.*(J~=K)-(J<K))),m*n,n);

Roger Stafford

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