Article View: comp.soft-sys.matlab
Article #121Re: Combining vectors
From: Douglas Gray Stephens
Date: Wed, 27 Jan 1993 12:08
Date: Wed, 27 Jan 1993 12:08
44 lines
2134 bytes
2134 bytes
In article <1k5hf2INNlgs@shelley.u.washington.edu> phantom@stein2.u.washington.edu (The Phantom) writes: In article <1993Jan26.182616.11550@vax.oxford.ac.uk> reese@vax.oxford.ac.uk writes: > > >I want to take two vectors and combine them, e.g. >[ a b c d e] and [ f g h i j ] >become [ a f b g c h d i e j ] > say a=[ a b c d e ] and b=[ f g h i j ] -- an easy way to do this would be using ... len=length(a); %assuming length(a), length(b) are equal FOR count=1:len, c((count*2)-1)=a(count); c((count*2)=b(count); END One of the advantages of matlab is the vector subscripts (do loops are not very efficient, and can normally be avioded), a better approach is:- (again assuming that the vectors are the same length, and called a and b) c([1:2:2*length(a),2:2:2*length(a)])=[a,b]; A more general solution for vectors of different lengths is:- i=length(a);j=length(b); if i>j c([1:2:2*j,j+[j+1:i],2:2:(2*j)])=[a,b]; else c([1:2:2*i,2:2:(2*i),i+[i+1:j]])=[a,b]; end -- ---------------------------------------------------------------------------- Dr. Douglas GRAY STEPHENS e-mail : gray@scr.slb.com Schlumberger Cambridge Research Tel/fax: 0223-325303 / 0223-311830 P.O.Box 153, Cambridge CB3 0HG, England (International prefix for UK: 44) ============================================================================
Technical Details
Message-ID:
<GRAY.93Jan27120836@snow.scr.slb.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!UB.com!pacbell.com!ames!agate!usenet.ins.cwru.edu!gatech!paladin.american.edu!howland.reston.ans.net!usc!cs.utexas.edu!geraldo.cc.utexas.edu!slcs.slb.com!leo.asc.slb.com!leo.asc.slb.com!grayReferences:
<1993Jan26.182616.11550@vax.oxford.ac.uk> <1k5hf2INNlgs@shelley.u.washington.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!UB.com!pacbell.com!ames!agate!usenet.ins.cwru.edu!gatech!paladin.american.edu!howland.reston.ans.net!usc!cs.utexas.edu!geraldo.cc.utexas.edu!slcs.slb.com!leo.asc.slb.com!leo.asc.slb.com!gray From: gray@snow.scr.slb.com (Douglas Gray Stephens) Newsgroups: comp.soft-sys.matlab Subject: Re: Combining vectors Date: 27 Jan 93 12:08:36 Organization: Schlumberger Cambridge Research Lines: 44 Message-ID: <GRAY.93Jan27120836@snow.scr.slb.com> References: <1993Jan26.182616.11550@vax.oxford.ac.uk> <1k5hf2INNlgs@shelley.u.washington.edu> NNTP-Posting-Host: snow.scr.slb.com In-reply-to: phantom@stein2.u.washington.edu's message of 27 Jan 1993 08:34:42 GMT