Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old September 14th, 2009, 05:55 PM
Newbie
 
Join Date: Jan 2009
Posts: 15
Country:
Thanks: 6
Thanked 0 Times in 0 Posts
mjlaz is on a distinguished road
Default 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
Reply With Quote
Advertisement
 
  #2  
Old September 14th, 2009, 09:34 PM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,262
Country:
Thanks: 777
Thanked 3,988 Times in 3,220 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

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
__________________
Truth does not change because it is, or is not, believed by a majority of the people.

Giordano Bruno
Reply With Quote
The following users thank CaptainBlack for this useful post:
Donate to MHF
  #3  
Old September 15th, 2009, 12:39 AM
Member
 
Join Date: Mar 2007
Posts: 143
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

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 15th, 2009 at 12:42 AM. Reason: fix code tags
Reply With Quote
The following users thank elbarto for this useful post:
Donate to MHF
  #4  
Old September 15th, 2009, 12:32 PM
Newbie
 
Join Date: Jan 2009
Posts: 15
Country:
Thanks: 6
Thanked 0 Times in 0 Posts
mjlaz is on a distinguished road
Default

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!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 01:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.