# Re-ordering a vector... (MATLAB)

• Sep 14th 2009, 04:55 PM
mjlaz
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
• Sep 14th 2009, 08:34 PM
CaptainBlack
Quote:

Originally Posted by mjlaz
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
• Sep 14th 2009, 11:39 PM
elbarto
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.
• Sep 15th 2009, 11:32 AM
mjlaz
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!

(Happy)

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!