Date: Feb 6, 2013 2:34 PM
Subject: Re: Writing a function with while and for loops
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
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.