Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Writing a function with while and for loops
Replies: 2   Last Post: Feb 6, 2013 2:41 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,159
Registered: 6/7/07
Re: Writing a function with while and for loops
Posted: Feb 6, 2013 2:34 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 2/6/2013 12:18 PM, Sara wrote:
> I am asked create a function of 2 inputs, the first defining a vector of
> random numbers and the second a scalar that sorts the vector in
> ascending if the scalar is 1 and descending order if the scalar value is
> 2. If only one input is given the function should sort in ascending
> order. The function is supposed to use a for loop that is nested in a
> while loop. The for loop should compare the vector value by value,
> switching the two as necessary to complete the ascending order. The
> while loop stops the function once the ascending or descending condition
> is met.
>
> So far I don't have much, but here it is.
>
>
> function BubbleSort = ScottS_BubbleSort(x,y)
> %UNTITLED3 Summary of this function goes here
> % Detailed explanation goes here
>
> while for k=x
> if k(1) >k(2) %if statement to compare and switch values
>
> end
> end
> end
> end


Well, you're probably going to have to have make more of a stab at it
than that...there are a zillion implementations of bubble sort you can
look at to understand the ideas--when you have a specific Matlab
implementation problem, then you'll likely get some help on that but on
an open-ended begging mission, not so much.

--



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.