🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.soft-sys.matlab
7 messages
7 total messages Started by reese Tue, 26 Jan 1993 18:26
Combining vectors
#116
Author: reese
Date: Tue, 26 Jan 1993 18:26
10 lines
650 bytes

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 ]

Does anyone know a quick and easy way to do this, and the reverse of
it?  Thanks in advance for your help!

Jason
Re: Combining vectors
#106
Author: Joe Barsugli
Date: Wed, 27 Jan 1993 01:51
35 lines
1743 bytes
In article <1k5hf2INNlgs@shelley.u.washington.edu>
phantom@stein2.u.washington.edu (The Phantom) writes in response to:

>   reese@vax.oxford.ac.uk, who wrote:
>
>   >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


How about a method which avoids the FOR loop:

c = [a;b];c = c(:)'  to go from a = [a b c ...] b = [f g h  ...] to 
                     c = [a f b g c h ...]

and a = c(1:2:length(c));b = c(2:2:length(c)); to unscramble.


Joe (joeb@atmos.washington.edu)
Re: Combining vectors
#108
Author: The Phantom
Date: Wed, 27 Jan 1993 08:34
22 lines
1203 bytes
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
Re: Combining vectors
#121
Author: Douglas Gray Stephens
Date: Wed, 27 Jan 1993 12:08
44 lines
2134 bytes
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


One of the advantages of matlab is the vector subscripts (do loops are
not very efficient, and can normally be avioded), a better approach
is:-

(again assuming that the vectors are the same length, and called a and
b)

c([1:2:2*length(a),2:2:2*length(a)])=[a,b];

A more general solution for vectors of different lengths is:-

i=length(a);j=length(b);
if i>j
 c([1:2:2*j,j+[j+1:i],2:2:(2*j)])=[a,b];
else
 c([1:2:2*i,2:2:(2*i),i+[i+1:j]])=[a,b];
end  

--
----------------------------------------------------------------------------
Dr. Douglas GRAY STEPHENS                e-mail : gray@scr.slb.com   
Schlumberger Cambridge Research          Tel/fax: 0223-325303 / 0223-311830
P.O.Box 153, Cambridge CB3 0HG, England  (International prefix for UK: 44)
============================================================================
Re: Combining vectors
#85
Author: Weizheng Wang
Date: Wed, 27 Jan 1993 14:47
46 lines
1864 bytes
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.
Re: Combining vectors
#102
Author: Ron L. Dilsavor
Date: Wed, 27 Jan 1993 15:58
33 lines
1606 bytes
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 to avoid using slow FOR loops if you can.
Here is a neat way which is faster

c(2:2:2*length(b))=b;
c(1:2:length(c))=a;

Ron Dilsavor
dilsavor@ee.eng.ohio-state.edu
Re: Combining vectors
#119
Author: Jeff Wilkinson
Date: Wed, 27 Jan 1993 21:35
49 lines
2014 bytes
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 ]
>
>Does anyone know a quick and easy way to do this, and the reverse of
>it?  Thanks in advance for your help!
>
>Jason


Jason,

Try this for 2 equal length input vectors:

>> v1 = [1 2 3];                  % create a vector for each
>> v2 = [4 5 6];                  %  operand
>> inx = 1:length([v1,v2]);       % create an indexing vector equal
>> inx = rem(inx,2);              %  to the summed length
>> result = zeros(size(inx));     % allocate the result vector
>> result(inx) = v1;              % put the v1 at odd indices
>> result(~inx) = v2;             % fill in the even with v2
>> result

result =
     1    4    2    5    3    6

>> result(inx)                    % retrieve v1

result =

     1    2    3

>> result(~inx)                   % retrieve v2

result =

     4    5    6


Hope this helps,

------------------------------------------------------------------------
Jeff Wilkinson                          Internet: wilk@medtronic.com
Medtronic, Inc.                         Voice +1-612-574-3770
7000 Central Ave NE                     FAX   +1-612-574-4306
Minneapolis, MN  55432  
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads