Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Rearranging vectors Matlab

  1. Sep 19, 2010 #1
    I'm having trouble understanding how to do a perfect shuffle of a vector. I'm supposed to split the vector in half then the 1st element is the 1st element in the first half and the 2nd element is the 1st element of the 2nd half, and so on.

    For example, if I have [1 2 3 4 5 6]
    then I need to rearrange it into [1 4 2 5 3 6]

    Can someone at least help me with the pseudo-code? I been thinking about this a few days with no luck. I am only allowed a few functions like "find" or "length". Nothing complex.
  2. jcsd
  3. Sep 20, 2010 #2


    User Avatar
    Gold Member

    bah, messed up. I figured this out a couple months ago. Will repost once I confirm.
    Last edited: Sep 20, 2010
  4. Sep 20, 2010 #3


    User Avatar
    Gold Member

    %it has to be an even length vector, but:

    N = length(x)/2
    y = zeros(N,1)

    for n = 1:N

    y(2*n-1) = n
    y(2*n) = N+n
  5. Sep 20, 2010 #4
    Can you do this without using for loops? We haven''t covered that yet. I may have to ask the professor. Its surprising because even the TA had no clue how to do this with the material we have covered so far.

  6. Sep 20, 2010 #5


    User Avatar
    Gold Member

    Wow, I can't thing of how you do this without a loop unless you exacty knew your input.

    If I have any insights, I'll share them with you.
  7. Sep 20, 2010 #6
    I like pythag's answer better, but something like this would work.


    Probably other ways of doing it using the reshapes and row/column swaps, etc.
  8. Sep 20, 2010 #7
    Can you explain this to me since I never used loops before? Also, any particular reason why you specify Nx1 zero vector rather than 1xN vector?
  9. Sep 21, 2010 #8


    User Avatar
    Gold Member

    A for loop just goes through all the lines between FOR and END, changing the specified value every time, so you can base your code on that evolving number.

    1xN or Nx1 is arbitrary in general. You may have a specific preference depending on the rest of your code. They're equivalent though in the absence of conflicting comparisons.
  10. Mar 2, 2011 #9

    your problem can be solved by this way

    %let c be the vector which has to be rearranged
    % assuming size of c to be even

    % d is the vector where we store the rearranged vector

    d = zeros(size(c));
    d(1:2:end) = c(1:end/2);
    d(2:2:end) = c(end/2+1,end);

    %thats all you need to do ;)

    Hope it will help!

    http://www.chhitizbuchasia.com" [Broken]
    Last edited by a moderator: May 5, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Rearranging vectors Matlab Date
MATLAB vector multiplication Aug 28, 2017
Matlab A Q on multiplying two vectors Mar 25, 2017
New to Matlab, help with vectors Feb 20, 2016
How to add normal vectors/quivers to sphere in matlab? Dec 20, 2015
Extract and rearrange data in Matlab May 10, 2013