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

  1. Path: sparky!uunet!news.univie.ac.at!scsing.switch.ch!univ-lyon1.fr!ghost.dsi.unimi.it!rpi!gatech!usenet.ins.cwru.edu!agate!spool.mu.edu!howland.reston.ans.net!usc!usc!not-for-mail
  2. From: wang@nyquist.usc.edu (Weizheng Wang)
  3. Newsgroups: comp.soft-sys.matlab
  4. Subject: Re: Combining vectors
  5. Date: 27 Jan 1993 06:47:57 -0800
  6. Organization: University of Southern California, Los Angeles, CA
  7. Lines: 47
  8. Message-ID: <1k67atINNb9p@nyquist.usc.edu>
  9. References: <1993Jan26.182616.11550@vax.oxford.ac.uk> <1k5hf2INNlgs@shelley.u.washington.edu>
  10. NNTP-Posting-Host: nyquist.usc.edu
  11.  
  12. In article <1k5hf2INNlgs@shelley.u.washington.edu> phantom@stein2.u.washington.edu (The Phantom) writes:
  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. >
  21. >say a=[ a b c d e ] and b=[ f g h i j ] -- an easy way to do this would be
  22. >using ...
  23. >
  24. >len=length(a);            %assuming length(a), length(b) are equal
  25. >FOR count=1:len,
  26. >    c((count*2)-1)=a(count);
  27. >    c((count*2)=b(count);
  28. >END
  29. >
  30. >that seems to work, anyway. just work with the commands for picking out 
  31. >certain matrix spots and FOR loops, and you should be able to comprise 
  32. >something that works both ways.
  33. >
  34. >mt
  35. >
  36.  
  37. Try the following:
  38.  
  39. % assignment of your vector
  40. x = ['a' 'b' 'c' 'd' 'e'];
  41. y = ['f' 'g' 'h' 'i' 'j'];
  42. % assign an index vector
  43. ind = [1:length(x)];
  44. %assign the vector
  45. z(ind*2-1) = x;
  46. z(ind*2) = y;
  47.  
  48. %reverse assignment
  49. %assign an index in a reverse way:
  50. ind2 = [length(x) : -1 : 1];
  51. %assign the vector
  52. z2(ind*2-1) = x(ind2);
  53. z2(ind*2) = y(ind2);
  54.  
  55.  
  56. Actually, the contents of ind and ind2 can be any number in the index range.
  57. I believe the example shows the most efficient and flexible way to use MATLAB.
  58.  
  59.