🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.soft-sys.matlab
Article #107

Re: Combining vectors

#107
From: Mark E. Kotanchek
Date: Wed, 27 Jan 1993 15:46
79 lines
2781 bytes
In article <1k67atINNb9p@nyquist.usc.edu> wang@nyquist.usc.edu (Weizheng  
Wang) writes:
> 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
> >
> >that seems to work, anyway. just work with the commands for picking out 
> >certain matrix spots and FOR loops, and you should be able to comprise 
> >something that works both ways.
> >
> >mt
> >
> 
> Try the following:
> 
> % assignment of your vector
> x = ['a' 'b' 'c' 'd' 'e'];
> y = ['f' 'g' 'h' 'i' 'j'];
> % assign an index vector
> ind = [1:length(x)];
> %assign the vector
> z(ind*2-1) = x;
> z(ind*2) = y;
> 
> %reverse assignment
> %assign an index in a reverse way:
> ind2 = [length(x) : -1 : 1];
> %assign the vector
> z2(ind*2-1) = x(ind2);
> z2(ind*2) = y(ind2);
> 
> 
> Actually, the contents of ind and ind2 can be any number in the index  
range.
> I believe the example shows the most efficient and flexible way to use  
MATLAB.

Actually, the easiest way to acomplish the desired rearrangement in matlab  
is via

   x=[a,b,c,d,e];   % definition of matrices
   y=[f,g,h,i,j];
   z=[x;y];         % build a new 2x5 matrix of x over y
   z=reshape(z,1,10); % and we have the desired form!

Of course, we could combine commands and make it a little more general,

    z=reshape([x;y],1,length([x;y]))

but you get the general idea. The first rule of matlab is to "exploit your  
data structures" and the second rule is that "there is probably an easy  
way to do things". The third rule is to "use functions and scripts--NOT  
the command line" if you want to avoid irreproducible results.

Mark.
--
Mark Kotanchek
Guidance & Control Dept - 363 ASB
Applied Research Lab/Penn State
P.O. Box 30
State College, PA 16804
Technical Details
Message-ID: <uw21H&v06c@atlantis.psu.edu>
Path: arc1-ca.pugleaf.net!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
References: <1k67atINNb9p@nyquist.usc.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.soft-sys.matlab
Path: arc1-ca.pugleaf.net!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
From: mek@guinan.psu.edu (Mark E. Kotanchek)
Subject: Re: Combining vectors
Message-ID: <uw21H&v06c@atlantis.psu.edu>
Sender: news@atlantis.psu.edu (Usenet)
Organization: Penn State Center for Academic Computing
References: <1k67atINNb9p@nyquist.usc.edu>
Date: Wed, 27 Jan 93 15:46:57 GMT
Lines: 80