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:
efficient matrix generation

Subject: efficient matrix generation

From: Dimitar Dimitrov

Date: 11 Feb, 2010 23:08:04

Message: 1 of 4

Hello,
I would like to generate an NxN matrix with the following structure (for N=6)

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

Is there a way to do this without for loops?

Thanks,
Dimitar

Subject: efficient matrix generation

From: Darren Rowland

Date: 12 Feb, 2010 01:22:20

Message: 2 of 4

Try the following

E = triu(ones(6),1);
E(E==1) = 1:15

Hth,
Darren

Subject: efficient matrix generation

From: Matt Fig

Date: 12 Feb, 2010 02:29:21

Message: 3 of 4

"Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <hl2agc$jkf$1@fred.mathworks.com>...
> Try the following
>
> E = triu(ones(6),1);
> E(E==1) = 1:15
>
> Hth,
> Darren



Or, rather

E = tril(ones(N),-1);
E(logical(E)) = 1:((N-1)*N/2);
E = E.'

Subject: efficient matrix generation

From: Dimitar Dimitrov

Date: 12 Feb, 2010 09:35:05

Message: 4 of 4

Hi
Thanks for the solution
Dimitar

> E = tril(ones(N),-1);
> E(logical(E)) = 1:((N-1)*N/2);
> E = E.'

Tags for 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