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

  1. Newsgroups: comp.soft-sys.matlab
  2. Path: sparky!uunet!news.univie.ac.at!scsing.switch.ch!univ-lyon1.fr!ghost.dsi.unimi.it!rpi!usc!howland.reston.ans.net!atlantis.psu.edu!news
  3. From: mek@guinan.psu.edu (Mark E. Kotanchek)
  4. Subject: Re: Combining vectors
  5. Message-ID: <uw21H&v06c@atlantis.psu.edu>
  6. Sender: news@atlantis.psu.edu (Usenet)
  7. Organization: Penn State Center for Academic Computing
  8. References: <1k67atINNb9p@nyquist.usc.edu>
  9. Date: Wed, 27 Jan 93 15:46:57 GMT
  10. Lines: 80
  11.  
  12. In article <1k67atINNb9p@nyquist.usc.edu> wang@nyquist.usc.edu (Weizheng  
  13. Wang) writes:
  14. > In article <1k5hf2INNlgs@shelley.u.washington.edu>  
  15. phantom@stein2.u.washington.edu (The Phantom) writes:
  16. > >In article <1993Jan26.182616.11550@vax.oxford.ac.uk>  
  17. reese@vax.oxford.ac.uk writes:
  18. > >>
  19. > >>
  20. > >>I want to take two vectors and combine them, e.g.
  21. > >>[ a b c d e]  and [ f g h i j ] 
  22. > >>become  [ a f b g c h d i e j ]
  23. > >>
  24. > >
  25. > >say a=[ a b c d e ] and b=[ f g h i j ] -- an easy way to do this would  
  26. be
  27. > >using ...
  28. > >
  29. > >len=length(a);            %assuming length(a), length(b) are  
  30. equal
  31. > >FOR count=1:len,
  32. > >    c((count*2)-1)=a(count);
  33. > >    c((count*2)=b(count);
  34. > >END
  35. > >
  36. > >that seems to work, anyway. just work with the commands for picking out 
  37. > >certain matrix spots and FOR loops, and you should be able to comprise 
  38. > >something that works both ways.
  39. > >
  40. > >mt
  41. > >
  42. > Try the following:
  43. > % assignment of your vector
  44. > x = ['a' 'b' 'c' 'd' 'e'];
  45. > y = ['f' 'g' 'h' 'i' 'j'];
  46. > % assign an index vector
  47. > ind = [1:length(x)];
  48. > %assign the vector
  49. > z(ind*2-1) = x;
  50. > z(ind*2) = y;
  51. > %reverse assignment
  52. > %assign an index in a reverse way:
  53. > ind2 = [length(x) : -1 : 1];
  54. > %assign the vector
  55. > z2(ind*2-1) = x(ind2);
  56. > z2(ind*2) = y(ind2);
  57. > Actually, the contents of ind and ind2 can be any number in the index  
  58. range.
  59. > I believe the example shows the most efficient and flexible way to use  
  60. MATLAB.
  61.  
  62. Actually, the easiest way to acomplish the desired rearrangement in matlab  
  63. is via
  64.  
  65.    x=[a,b,c,d,e];   % definition of matrices
  66.    y=[f,g,h,i,j];
  67.    z=[x;y];         % build a new 2x5 matrix of x over y
  68.    z=reshape(z,1,10); % and we have the desired form!
  69.  
  70. Of course, we could combine commands and make it a little more general,
  71.  
  72.     z=reshape([x;y],1,length([x;y]))
  73.  
  74. but you get the general idea. The first rule of matlab is to "exploit your  
  75. data structures" and the second rule is that "there is probably an easy  
  76. way to do things". The third rule is to "use functions and scripts--NOT  
  77. the command line" if you want to avoid irreproducible results.
  78.  
  79. Mark.
  80. --
  81. Mark Kotanchek
  82. Guidance & Control Dept - 363 ASB
  83. Applied Research Lab/Penn State
  84. P.O. Box 30
  85. State College, PA 16804
  86.  
  87.