Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Ronix

macrumors member
Original poster
Jan 31, 2008
54
0
For the professionals among us, I cannot figure this out for the life of me and I know it is simple....

I want to combine two vectors a=[1 3 5 7 9 11] and b=[20 40 60] into one vector c, so that c=[1 20 3 40 5 60 7 9 11]. If you cannot tell it alternates vectors until one runs out then it just finishes the with the rest of the other vectors values.

I know if I do c=[a,b] it does the [ 1 3 5 7 9 11 20 40 60] but I cannot get it to do the above.

I am pretty sure the length command is involved somehow.

Any help would be aprreciated! I am a very frustrated, but I know it is extremely easy (major brain fart!)
 
Are the lengths of the two vectors always going to be the same as in your example? I.e. length 6 and length 3? If so you could just brute force it by building a new vector by calling out the values of each vector dimension explicitly. For example, c=[a[1] b[1] a[2] b[2] a[3] b[3] a[4:6]]

Not sure how to do this in a general sense where the two lengths could vary.
 
Gaucho,

I guess I didn't think of it that way.... guess I really did have a brain fart. That would work I suppose very easily. I will do that for the script I am writing for the time being. Thanks!

If anyone else has any other ideas incase I do need to change the length of the vectors I would still appreciate it!


Ronix
 
Gaucho,

I guess I didn't think of it that way.... guess I really did have a brain fart. That would work I suppose very easily. I will do that for the script I am writing for the time being. Thanks!

If anyone else has any other ideas incase I do need to change the length of the vectors I would still appreciate it!


Ronix

The more formal way to do this would probably be in a loop, where you assign indices to each vector, like:

i=1;
j=1;
for i<6
for j<3
c=[a b[j]]


then let it increment the vector for you.
 
The more formal way to do this would probably be in a loop, where you assign indices to each vector, like:

i=1;
j=1;
for i<6
for j<3
c=[a b[j]]


then let it increment the vector for you.


This is much cleaner, nevertheless if you are not always the same vectors length, you could replace "6" and "3" with "size(a)" and "size(b)".

Hope it helps
 
Cool. I would have probably tried something with reshape.

I use it all the time to do the opposite, take vectors of real and imaginary parts [r i r i r i r i r i r i] to complex numbers [c c c c c c].

i.e. pull out two vectors of the same length as the short one from the two vectors, interleave them with reshape, then concatenate the remaining vector or something along those lines.

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.