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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com