home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / softsys / matlab / 222 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  2.0 KB

  1. Path: 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
  2. From: gray@snow.scr.slb.com (Douglas Gray Stephens)
  3. Newsgroups: comp.soft-sys.matlab
  4. Subject: Re: Combining vectors
  5. Date: 27 Jan 93 12:08:36
  6. Organization: Schlumberger Cambridge Research
  7. Lines: 44
  8. Message-ID: <GRAY.93Jan27120836@snow.scr.slb.com>
  9. References: <1993Jan26.182616.11550@vax.oxford.ac.uk>
  10.     <1k5hf2INNlgs@shelley.u.washington.edu>
  11. NNTP-Posting-Host: snow.scr.slb.com
  12. In-reply-to: phantom@stein2.u.washington.edu's message of 27 Jan 1993 08:34:42 GMT
  13.  
  14. In article <1k5hf2INNlgs@shelley.u.washington.edu> phantom@stein2.u.washington.edu (The Phantom) writes:
  15.  
  16.    In article <1993Jan26.182616.11550@vax.oxford.ac.uk> reese@vax.oxford.ac.uk writes:
  17.    >
  18.    >
  19.    >I want to take two vectors and combine them, e.g.
  20.    >[ a b c d e]  and [ f g h i j ] 
  21.    >become  [ a f b g c h d i e j ]
  22.    >
  23.  
  24.    say a=[ a b c d e ] and b=[ f g h i j ] -- an easy way to do this would be
  25.    using ...
  26.  
  27.    len=length(a);            %assuming length(a), length(b) are equal
  28.    FOR count=1:len,
  29.        c((count*2)-1)=a(count);
  30.        c((count*2)=b(count);
  31.    END
  32.  
  33.  
  34. One of the advantages of matlab is the vector subscripts (do loops are
  35. not very efficient, and can normally be avioded), a better approach
  36. is:-
  37.  
  38. (again assuming that the vectors are the same length, and called a and
  39. b)
  40.  
  41. c([1:2:2*length(a),2:2:2*length(a)])=[a,b];
  42.  
  43. A more general solution for vectors of different lengths is:-
  44.  
  45. i=length(a);j=length(b);
  46. if i>j
  47.  c([1:2:2*j,j+[j+1:i],2:2:(2*j)])=[a,b];
  48. else
  49.  c([1:2:2*i,2:2:(2*i),i+[i+1:j]])=[a,b];
  50. end  
  51.  
  52. --
  53. ----------------------------------------------------------------------------
  54. Dr. Douglas GRAY STEPHENS                e-mail : gray@scr.slb.com   
  55. Schlumberger Cambridge Research          Tel/fax: 0223-325303 / 0223-311830
  56. P.O.Box 153, Cambridge CB3 0HG, England  (International prefix for UK: 44)
  57. ============================================================================
  58.