Thread Subject: Inserting elements in a vector

Subject: Inserting elements in a vector

From: somdeb

Date: 24 Sep, 2007 15:27:29

Message: 1 of 2

I have a vector v1 of L1 elements. I need to insert elements of
another vector v2 of L2 elements (L2<L1) into known positions of v1.

What is the fastest way to do this? Right now I preallocate L1+L2
elements for v1 and for every position I have to insert a sample, I
right-shift all the elements of v1 to the right of that position and
then insert the value. This is taking a long time.

Subject: Inserting elements in a vector

From: us

Date: 24 Sep, 2007 15:51:31

Message: 2 of 2

somdeb:
<SNIP vec acrobatics...

one of the many solutions

% the data
     v=1:10;
     w=[-10,-20,-30];
     pos=[2,6,7];
% the engine
     tf=false(1,numel(v)+numel(w));
     r=double(tf);
     tf(pos)=true;
     r(tf)=w;
     r(~tf)=v;
% the result
     r

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
logical indexing us 24 Sep, 2007 11:55:08
index us 24 Sep, 2007 11:55:08
code us 24 Sep, 2007 11:55:07
rssFeed for this Thread

Contact us at files@mathworks.com