Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: index end-point to matrix
Replies: 6   Last Post: Feb 4, 2013 11:24 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Jos

Posts: 1,266
Registered: 10/24/08
Re: index end-point to matrix
Posted: Feb 1, 2013 4:51 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.