Friday, November 30, 2007

Matlab Tutorial

Bubble Sorting



Welcome back to Matlab Tutorial, last week I didn't post an article but I am back this week to give some more fun tips and commands. For this week, I was going to do kind of a fun program that is useful to help organize and manipulate data points. It is what some call a bubble sort. This is where all the values that you are given can be sorted from increasing values to decreasing values or vise verse. It is a simple nested for loop that can help you when you want to find the median of a data set or the mode of the set of values.





So to write this program, you must start with loading your data. If you hard code the data in your program that's fine too. Basically however you have data load it or store it as a variable. After setting a variable to it find the number of data points you will be using for the sort using some kind of length/size command. You now write a nested for loop to rearrange the values so that they are in ascending order or descending order. For my example I will put them in order from least value to greatest value. The loops with be written to tell Matlab to compare each value to the one next to it. If the latter value is less than the former, then the values switch. The first loop's index should be from 1 to the number of values you are given. The second for loop's index will go from one to one less than the number the first index is in. This is prevent the loops from going over already compared numbers that were already sorted. The final nested function in this loop is an if statement. This is where it compares the second index term of the data set to the term right after the latter. It sees if the second term is less than the first. If that's a true statement, then it switches the values. It does this through assigning the first value to a random variable name. (Which I give as SwitchX) Then the first value because the second value and the second becomes the extra, or first, value. Let's show this with code:

clc
X = [9 10 5 3 6 7 1 4 2 8]
Length = length(X);< X(B);
for A = 1 : Length
for B = 1 : Length - A
if X(B+1) < X(B)
SwitchX = X(B);
X(B) = X(B+1);
X(B+1) = SwitchX;
end
end
end
X



That's all I got now. Stay tune for the possible LAST entry next week! Don't miss the stunning conclusion of Matlab Tutorial!

Resources: While Loop Help with Cyclismo, HTML Tags at Web Source

2 comments:

Alex Strinka said...

Aw, come one. Quicksort isn't much harder to do, and it's so much faster (at least for large arrays. It's a recursive function, but it's not bad.

In words, what it does is pick a pivot (any random data point will work). Then, it compares every other element with the pivot. If it's larger than the pivot, it moves it to the right of the pivot, if it's smaller than the pivot, it moves it to the left of the pivot. Then it runs itself on the list to the left of the pivot, and on the right of the pivot.

In pseudocode, since I don't actually know Matlab:
function quicksort(array)
var list lessOrEqual, greater
if length(array) ≤ 1
return array
select a pivot value pivot from array
for each x in array
if x ≤ pivot then add x to lessOrEqual
if x > pivot then add x to greater
return concatenate(quicksort(lessOrEqual), quicksort(greater))

Anonymous said...

Hey! Do you know if theу make any рlugіns tο help with Sеaгch Еngine
Optimizatіon? Ι'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good succеss.
If you knoω of any plеase shaгe.
Ϲheerѕ!

Feel freе to surf to my website: cialis