home *** CD-ROM | disk | FTP | other *** search
- 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
- From: wang@nyquist.usc.edu (Weizheng Wang)
- Newsgroups: comp.soft-sys.matlab
- Subject: Re: Combining vectors
- Date: 27 Jan 1993 06:47:57 -0800
- Organization: University of Southern California, Los Angeles, CA
- Lines: 47
- Message-ID: <1k67atINNb9p@nyquist.usc.edu>
- References: <1993Jan26.182616.11550@vax.oxford.ac.uk> <1k5hf2INNlgs@shelley.u.washington.edu>
- NNTP-Posting-Host: nyquist.usc.edu
-
- 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.
-
-