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:
index end-point to matrix

Subject: index end-point to matrix

From: Naresh Pai

Date: 30 Jan, 2013 21:38:08

Message: 1 of 7

I have a vector with indices, for e.g.

b = [1 4 7]

Using these indices, I am trying to develop a matrix that looks like this
a =[1 0 0;
      2 3 4;
      5 6 7]

In other words, the indices variable (b) indicates end-points of the matrix a for each row. This looks simple but been trying to figure out how to generalize the code when b could be of different sizes. Any hints would be appreciated.

Thanks,
Naresh

Subject: index end-point to matrix

From: Jos (10584)

Date: 31 Jan, 2013 10:20:08

Message: 2 of 7

"Naresh Pai" wrote in message <kec3s0$gl9$1@newscl01ah.mathworks.com>...
> I have a vector with indices, for e.g.
>
> b = [1 4 7]
>
> Using these indices, I am trying to develop a matrix that looks like this
> a =[1 0 0;
> 2 3 4;
> 5 6 7]
>
> In other words, the indices variable (b) indicates end-points of the matrix a for each row. This looks simple but been trying to figure out how to generalize the code when b could be of different sizes. Any hints would be appreciated.
>
> Thanks,
> Naresh

I got confused ... what would A look like if B = [4 1 6 2] for instance?

~ Jos

Subject: index end-point to matrix

From: Naresh Pai

Date: 31 Jan, 2013 15:36:08

Message: 3 of 7

"Jos (10584)" wrote in message <kedggo$ild$1@newscl01ah.mathworks.com>...
> "Naresh Pai" wrote in message <kec3s0$gl9$1@newscl01ah.mathworks.com>...
> > I have a vector with indices, for e.g.
> >
> > b = [1 4 7]
> >
> > Using these indices, I am trying to develop a matrix that looks like this
> > a =[1 0 0;
> > 2 3 4;
> > 5 6 7]
> >
> > In other words, the indices variable (b) indicates end-points of the matrix a for each row. This looks simple but been trying to figure out how to generalize the code when b could be of different sizes. Any hints would be appreciated.
> >
> > Thanks,
> > Naresh
>
> I got confused ... what would A look like if B = [4 1 6 2] for instance?
>
> ~ Jos

Jos, Thanks for your post. B results from a cumsum operation, so it would either ascending (e.g. [1 3 5 9]), or remain constant at some elements ([1 2 2 4]). In the example you provided, the second and fourth element are descending. I have put few other examples to illustrate my requirement. Thanks.

1) b = [2 0 3 4]
    a = [1 2;
           0 0;
           3 0
           4 0]

2) b = [ 0 3 7 10]
    a = [0 0 0 0;
            1 2 3 0;
            4 5 6 7;
            8 9 10 0]

3) b = [5 0 0]
    a = [1 2 3 4 5;
           0 0 0 0 0;
           0 0 0 0 0]

Subject: index end-point to matrix

From: Jos (10584)

Date: 1 Feb, 2013 09:51:08

Message: 4 of 7

"Naresh Pai" wrote in message <kee318$sd5$1@newscl01ah.mathworks.com>...
> "Jos (10584)" wrote in message <kedggo$ild$1@newscl01ah.mathworks.com>...
> > "Naresh Pai" wrote in message <kec3s0$gl9$1@newscl01ah.mathworks.com>...
> > > I have a vector with indices, for e.g.
> > >
> > > b = [1 4 7]
> > >
> > > Using these indices, I am trying to develop a matrix that looks like this
> > > a =[1 0 0;
> > > 2 3 4;
> > > 5 6 7]
> > >
> > > In other words, the indices variable (b) indicates end-points of the matrix a for each row. This looks simple but been trying to figure out how to generalize the code when b could be of different sizes. Any hints would be appreciated.
> > >
> > > Thanks,
> > > Naresh
> >
> > I got confused ... what would A look like if B = [4 1 6 2] for instance?
> >
> > ~ Jos
>
> Jos, Thanks for your post. B results from a cumsum operation, so it would either ascending (e.g. [1 3 5 9]), or remain constant at some elements ([1 2 2 4]). In the example you provided, the second and fourth element are descending. I have put few other examples to illustrate my requirement. Thanks.
>
> 1) b = [2 0 3 4]
> a = [1 2;
> 0 0;
> 3 0
> 4 0]
>
> 2) b = [ 0 3 7 10]
> a = [0 0 0 0;
> 1 2 3 0;
> 4 5 6 7;
> 8 9 10 0]
>
> 3) b = [5 0 0]
> a = [1 2 3 4 5;
> 0 0 0 0 0;
> 0 0 0 0 0]

Ah, the problem did rang a bell. Here's a solution using an indexing trick and a special function that I wrote a couple of years ago:

b = [2 0 3 4] ;
b(b>0) = diff([0 b(b>0)])
a = double(nones(b))
a(a==1)=1:sum(a(:))
a = a.'

My function NONES can be found here:
http://www.mathworks.com/matlabcentral/fileexchange/10622

hth
~ Jos

Subject: index end-point to matrix

From: Bruno Luong

Date: 1 Feb, 2013 14:04:08

Message: 5 of 7

k = b > 0;
c = b(k);
l = diff([0 c]);
s = c(end);
c(end) = [];
i = zeros(1,s);
i(c+1) = 1;
i = 1+cumsum(i);
j = ones(1,s);
j(c+1) = 1-l(1:end-1);
j = cumsum(j);
a = zeros(length(b), max(j));
a(k,:) = accumarray([i(:) j(:)], (1:s)')

% Bruno

Subject: index end-point to matrix

From: Bruno Luong

Date: 2 Feb, 2013 09:41:06

Message: 6 of 7

Another method:

k = b > 0;
c = b(k);
l = diff([0 c]);
m = bsxfun(@le,(1:max(l))',l);
r = zeros(size(m));
r(m) = 1:c(end);
a = zeros(length(b), max(l));
a(k,:) = r'

% Bruno

Subject: index end-point to matrix

From: Naresh Pai

Date: 4 Feb, 2013 16:24:08

Message: 7 of 7

Bruno,

Both methods work for me but I think the second solution is a brilliant use of bsxfun. Thanks for your reply.

Naresh

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <keimvi$15k$1@newscl01ah.mathworks.com>...
> Another method:
>
> k = b > 0;
> c = b(k);
> l = diff([0 c]);
> m = bsxfun(@le,(1:max(l))',l);
> r = zeros(size(m));
> r(m) = 1:c(end);
> a = zeros(length(b), max(l));
> a(k,:) = r'
>
> % 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