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:
indexing into a matrix

Subject: indexing into a matrix

From: Joao Ejarque

Date: 5 Aug, 2011 11:56:11

Message: 1 of 5

I have a matrix M(n1,n2) of zeros.

I have a vector z of size (1,n1) with indices of columns in the space n2.

So if n1 = 3, and n2 = 7, for example z=[2 4 6];

I want to create the following matrix in the simplest way:

Q = [ 0 1 1 1 1 1 1;
         0 0 0 1 1 1 1;
         0 0 0 0 0 1 1];

where to the left of z i have zeros and to the right i have ones.

JE

Subject: indexing into a matrix

From: Bruno Luong

Date: 5 Aug, 2011 12:12:15

Message: 2 of 5

n1=3;
n2=7;
z=[2 4 6];

Q = cumsum(accumarray([1:n1; z]', 1, [n1 n2]),2)

Subject: indexing into a matrix

From: Joao Ejarque

Date: 5 Aug, 2011 12:19:11

Message: 3 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j1gmmv$avl$1@newscl01ah.mathworks.com>...
> n1=3;
> n2=7;
> z=[2 4 6];
>
> Q = cumsum(accumarray([1:n1; z]', 1, [n1 n2]),2)

outstanding. thank you.

Subject: indexing into a matrix

From: Bruno Luong

Date: 5 Aug, 2011 19:37:13

Message: 4 of 5

Another way:

% logical, cast to double if wished
Q = bsxfun(@ge,meshgrid(1:n2,1:n1),z(:))

Bruno

Subject: indexing into a matrix

From: Bruno Luong

Date: 5 Aug, 2011 20:01:29

Message: 5 of 5

Or even better:
 Q = bsxfun(@ge,1:n2,z(:))

% Bruno

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