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: Inserting values into middle of an array
Replies: 3   Last Post: Oct 12, 2013 6:55 PM

Advanced Search

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

Posts: 10
Registered: 10/11/13
Re: Inserting values into middle of an array
Posted: Oct 12, 2013 5:49 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

TideMan <mulgor@gmail.com> wrote in message <daff221e-5014-402c-abd7-18488b88f032@googlegroups.com>...
> On Sunday, October 13, 2013 8:50:08 AM UTC+13, David Quilligan wrote:
> > Hello,
> >
> >
> >
> > I have a column vector of which the length can vary. What I want to do is insert a certain value in between every second value of this vector. So far I am able to this with this code:
> >
> >
> >
> > offset = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
> >
> > i = ones(1,5) * 5;
> >
> > Ar1=[i];Pos1=2;
> >
> > Ar2=[i];Pos2=3;
> >
> > offset=[offset(1:(Pos1-1)) Ar1 offset(Pos1:(Pos2-1)) Ar2 offset(Pos1:end)];
> >
> >
> >
> > where I insert 5 fives into the original array for the 2nd and 4th positions. However if I continue like this my code will get very long and also since the length of the original array can change I'll be in trouble. Can anyone explain a way to do this by using a loop? I get very confused with the use of loops!

>
> How about:
> offset=[offset;5*ones(1,length(offset))];
> offset=offset(:)';


That will insert one 5 in every second position. However I want to insert more than just one value (127 actually) every second position. Is it possible to adjust your code to accomplish this? Thanks



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.