home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / softsys / matlab / 220 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.9 KB  |  62 lines

  1. Newsgroups: comp.soft-sys.matlab
  2. Path: sparky!uunet!charon.amdahl.com!amdahl!rtech!sgiblab!darwin.sura.net!bogus.sura.net!howland.reston.ans.net!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!mmm.serc.3m.com!pwcs!medtron!norway!jw7348
  3. From: jw7348@norway.medtronic.com (Jeff Wilkinson)
  4. Subject: Re: Combining vectors
  5. Message-ID: <1993Jan27.213533.12837@medtron.medtronic.com>
  6. Sender: news@medtron.medtronic.com (USENET News Administration)
  7. Nntp-Posting-Host: norway.inst.medtronic.com
  8. Organization: Medtronic, Inc., Minneapolis, MN
  9. References: <1993Jan26.182616.11550@vax.oxford.ac.uk>
  10. Date: Wed, 27 Jan 1993 21:35:33 GMT
  11. Lines: 49
  12.  
  13. In article <1993Jan26.182616.11550@vax.oxford.ac.uk> reese@vax.oxford.ac.uk writes:
  14. >
  15. >
  16. >I want to take two vectors and combine them, e.g.
  17. >[ a b c d e]  and [ f g h i j ] 
  18. >become  [ a f b g c h d i e j ]
  19. >
  20. >Does anyone know a quick and easy way to do this, and the reverse of
  21. >it?  Thanks in advance for your help!
  22. >
  23. >Jason
  24.  
  25.  
  26. Jason,
  27.  
  28. Try this for 2 equal length input vectors:
  29.  
  30. >> v1 = [1 2 3];                  % create a vector for each
  31. >> v2 = [4 5 6];                  %  operand
  32. >> inx = 1:length([v1,v2]);       % create an indexing vector equal
  33. >> inx = rem(inx,2);              %  to the summed length
  34. >> result = zeros(size(inx));     % allocate the result vector
  35. >> result(inx) = v1;              % put the v1 at odd indices
  36. >> result(~inx) = v2;             % fill in the even with v2
  37. >> result
  38.  
  39. result =
  40.      1    4    2    5    3    6
  41.  
  42. >> result(inx)                    % retrieve v1
  43.  
  44. result =
  45.  
  46.      1    2    3
  47.  
  48. >> result(~inx)                   % retrieve v2
  49.  
  50. result =
  51.  
  52.      4    5    6
  53.  
  54.  
  55. Hope this helps,
  56.  
  57. ------------------------------------------------------------------------
  58. Jeff Wilkinson                          Internet: wilk@medtronic.com
  59. Medtronic, Inc.                         Voice +1-612-574-3770
  60. 7000 Central Ave NE                     FAX   +1-612-574-4306
  61. Minneapolis, MN  55432  
  62.