Results 1 to 4 of 4

Math Help - Re-ordering a vector... (MATLAB)

  1. #1
    Newbie
    Joined
    Jan 2009
    Posts
    21

    Re-ordering a vector... (MATLAB)

    My assignment is to create a function that takes a vector of the form

    v = [3 9 1 8 5 4]

    and puts it in order from smallest to largest as in

    v = [1 3 4 5 8 9]

    So far, I've got this, but it only works on a vector of the form v = [a b].

    Code:
    function x = putinorder(p)
    
    a = length(p); 
    
    for i=1:a-1
        if p(a) < p(a-i)
            ex = p(a-i);
            p(a-i) = p(a);
            p(a) = ex;
        end
    x = p;
    end
    
    end
    
    >> putinorder([5,1])
    
    ans =
    
         1     5
    I am required to use a for loop. Any suggestions or tips? Thanks,

    Marc
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by mjlaz View Post
    My assignment is to create a function that takes a vector of the form

    v = [3 9 1 8 5 4]

    and puts it in order from smallest to largest as in

    v = [1 3 4 5 8 9]

    So far, I've got this, but it only works on a vector of the form v = [a b].

    Code:
    function x = putinorder(p)
    
    a = length(p); 
    
    for i=1:a-1
        if p(a) < p(a-i)
            ex = p(a-i);
            p(a-i) = p(a);
            p(a) = ex;
        end
    x = p;
    end
    
    end
    
    >> putinorder([5,1])
    
    ans =
    
         1     5
    I am required to use a for loop. Any suggestions or tips? Thanks,

    Marc
    Consider a function that finds the smallest element and puts it into the first position in the array (this has not been run so is almost certianly full of errors which you will have to elliminate):

    Code:
    function rv=smallstep(array)
    
      ll=length(array);
      sml=array(1);smidx=1;
    
      for idx=2 to ll
        if (array(idx)<sml)
           sml=array(idx);
           smidx=idx;
        end
      end
      iidxs=find(array~=sml);
      rv=[sml,array(iidxs)];
    Once you have this working use it to sort your array.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    This solution is not exactly what you were chasing (I missed the bit about the for loop before I got started), but it sorts through a array using recursion rather than looping.

    Code:
    function A = mhfQuickSort(myarray)
    %http://en.wikipedia.org/wiki/Sorting_algorithm
    %http://en.wikipedia.org/wiki/Quicksort
    Less = [];Greater = [];
    if length(myarray) <= 1
        A = myarray;
        return
    else
        Pivot = myarray(1);
        myarray(1) = [];
        for x = myarray
            if x <= Pivot
                Less = [Less x];
            else
                Greater = [Greater x];
            end
        end
        A   = [mhfQuickSort(Less) Pivot mhfQuickSort(Greater)];
        return
    end
    Example:
    Code:
    EDU>> A = round(10*rand(1,10))
    
    A =
    
         7     1     0     6     6    10     7     7     5     3
    
    EDU>> mhfQuickSort(A)
    
    ans =
    
         0     1     3     5     6     6     7     7     7    10
    
    EDU>>
    Regards Elbarto.
    Last edited by elbarto; September 14th 2009 at 11:42 PM. Reason: fix code tags
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Jan 2009
    Posts
    21
    Thanks for your input. Unfortunately, my class is pretty much beginner level in MATLAB programming so I don't know if I would have the courage to turn in such intricate functions that you guys wrote!



    At any rate, I figured it out!

    Code:
    function x = reordervector(v)
    
    a = length(v);
    
    for i=1:a-1
        for k=0:a-1
            if v(a-k) < v(a-i)
                ex = v(a-i);
                v(a-i) = v(a-k);
                v(a-k) = ex;
            end
        end
    end 
    x = v;
    end
    
    >> reordervector([9 6 8 4 5 3 2 1])
    
    ans =
    
         1     2     3     4     5     6     8     9
    
    >> reordervector([12:-1:0])
    
    ans =
    
         0     1     2     3     4     5     6     7     8     9    10    11    12
    
    >> reordervector([9 -1 3 3 -2 -4 8 9])
    
    ans =
    
        -4    -2    -1     3     3     8     9     9
    I went nuts when it worked.... people thought I was crazy. Thanks!
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab Vector Factorial Function
    Posted in the Math Software Forum
    Replies: 2
    Last Post: January 31st 2010, 06:39 AM
  2. Matlab function with vector inputs and matrix outputs
    Posted in the Math Software Forum
    Replies: 3
    Last Post: August 6th 2009, 09:49 PM
  3. Replies: 0
    Last Post: August 5th 2009, 12:51 PM
  4. Replies: 1
    Last Post: April 7th 2009, 08:52 AM
  5. Well-Ordering
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: October 15th 2008, 08:13 AM

Search Tags


/mathhelpforum @mathhelpforum