simple matlab help

Discussion in 'Mac Programming' started by Ronix, Sep 27, 2009.

  1. Ronix macrumors member

    Joined:
    Jan 31, 2008
    #1
    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!)
     
  2. gauchogolfer macrumors 603

    gauchogolfer

    Joined:
    Jan 28, 2005
    Location:
    American Riviera
    #2
    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.
     
  3. Ronix thread starter macrumors member

    Joined:
    Jan 31, 2008
    #3
    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
     
  4. gauchogolfer macrumors 603

    gauchogolfer

    Joined:
    Jan 28, 2005
    Location:
    American Riviera
    #4
    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.
     
  5. gonche1124 macrumors newbie

    Joined:
    Aug 6, 2009
    #5


    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
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    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
     

Share This Page