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:
Create a matrix.

Subject: Create a matrix.

From: Mahesh Ramaraj

Date: 10 Nov, 2010 00:22:04

Message: 1 of 4

Hi, I have some difficulty if forming the following matrix.
n = 3; (number of columns)
m = (n-1)*n; (number of rows)
when n =3, i need the following matrix:
A = [-1 1 0
       -1 0 1;
        1 -1 0;
        0 -1 1;
        1 0 -1;
        0 1 -1]

and when n = 4,
A = [-1 1 0 0;
        -1 0 1 0;
        -1 0 0 1;
         1 -1 0 0;
         0 -1 1 0;
         0 -1 0 1;
         1 0 -1 0;
         0 1 -1 0;
         0 0 -1 1;
         1 0 0 -1;
         0 1 0 -1;
         0 0 1 -1]

And when n increases, the matrix expands accordingly.
As you can there is a pattern for -1's, they appear consequently for n-1 rows and shifts by one column and the ones have change according to each row. It would be great if someone could help with this. I have spent quite a bit of time on this. Thanks for the help.

Subject: Create a matrix.

From: John D'Errico

Date: 10 Nov, 2010 01:13:04

Message: 2 of 4

"Mahesh Ramaraj" <mramaraj@purdue.edu> wrote in message <ibcojb$suv$1@fred.mathworks.com>...
> Hi, I have some difficulty if forming the following matrix.
> n = 3; (number of columns)
> m = (n-1)*n; (number of rows)
> when n =3, i need the following matrix:
> A = [-1 1 0
> -1 0 1;
> 1 -1 0;
> 0 -1 1;
> 1 0 -1;
> 0 1 -1]
>
> and when n = 4,
> A = [-1 1 0 0;
> -1 0 1 0;
> -1 0 0 1;
> 1 -1 0 0;
> 0 -1 1 0;
> 0 -1 0 1;
> 1 0 -1 0;
> 0 1 -1 0;
> 0 0 -1 1;
> 1 0 0 -1;
> 0 1 0 -1;
> 0 0 1 -1]
>
> And when n increases, the matrix expands accordingly.
> As you can there is a pattern for -1's, they appear consequently for n-1 rows and shifts by one column and the ones have change according to each row. It would be great if someone could help with this. I have spent quite a bit of time on this. Thanks for the help.

help nchoosek

(Think about it, before you just discard the idea, or
give up.)

John

Subject: Create a matrix.

From: Roger Stafford

Date: 10 Nov, 2010 05:47:03

Message: 3 of 4

"Mahesh Ramaraj" <mramaraj@purdue.edu> wrote in message <ibcojb$suv$1@fred.mathworks.com>...
> Hi, I have some difficulty if forming the following matrix.
> n = 3; (number of columns)
> m = (n-1)*n; (number of rows)
> when n =3, i need the following matrix:
> A = [-1 1 0
> -1 0 1;
> 1 -1 0;
> 0 -1 1;
> 1 0 -1;
> 0 1 -1]
>
> and when n = 4,
> A = [-1 1 0 0;
> -1 0 1 0;
> -1 0 0 1;
> 1 -1 0 0;
> 0 -1 1 0;
> 0 -1 0 1;
> 1 0 -1 0;
> 0 1 -1 0;
> 0 0 -1 1;
> 1 0 0 -1;
> 0 1 0 -1;
> 0 0 1 -1]
>
> And when n increases, the matrix expands accordingly.
> As you can there is a pattern for -1's, they appear consequently for n-1 rows and shifts by one column and the ones have change according to each row. It would be great if someone could help with this. I have spent quite a bit of time on this. Thanks for the help.
- - - - - - - - - - -
  Assuming it is this specific pattern with -1's, 1's, and 0's with n columns, do this:

 A = [repmat(-1,n-1,n-1);repmat(eye(n-1),n,1)];
 A = reshape([A(:);repmat(-1,n-1,1)],[],n);

Roger Stafford

Subject: Create a matrix.

From: Mahesh Ramaraj

Date: 10 Nov, 2010 14:30:26

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ibdbkn$8gd$1@fred.mathworks.com>...
> "Mahesh Ramaraj" <mramaraj@purdue.edu> wrote in message <ibcojb$suv$1@fred.mathworks.com>...
> > Hi, I have some difficulty if forming the following matrix.
> > n = 3; (number of columns)
> > m = (n-1)*n; (number of rows)
> > when n =3, i need the following matrix:
> > A = [-1 1 0
> > -1 0 1;
> > 1 -1 0;
> > 0 -1 1;
> > 1 0 -1;
> > 0 1 -1]
> >
> > and when n = 4,
> > A = [-1 1 0 0;
> > -1 0 1 0;
> > -1 0 0 1;
> > 1 -1 0 0;
> > 0 -1 1 0;
> > 0 -1 0 1;
> > 1 0 -1 0;
> > 0 1 -1 0;
> > 0 0 -1 1;
> > 1 0 0 -1;
> > 0 1 0 -1;
> > 0 0 1 -1]
> >
> > And when n increases, the matrix expands accordingly.
> > As you can there is a pattern for -1's, they appear consequently for n-1 rows and shifts by one column and the ones have change according to each row. It would be great if someone could help with this. I have spent quite a bit of time on this. Thanks for the help.
> - - - - - - - - - - -
> Assuming it is this specific pattern with -1's, 1's, and 0's with n columns, do this:
>
> A = [repmat(-1,n-1,n-1);repmat(eye(n-1),n,1)];
> A = reshape([A(:);repmat(-1,n-1,1)],[],n);
>
> Roger Stafford

Thanks Roger, that helped a lot.

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