Tuesday, December 30, 2014

Matlab Easter Eggs

In this post I have shared some of the easter eggs exist in Matlab. Matlab easter eggs consist of hidden pictures, games and jokes. Although all of them are not related to engineering, electronics or coding, it would be fun to try these commands in Matlab.

Open Matlab and type the following commands.
1. Type "image"


easter eggs image
2. Type "life" 

easter eggs life

3. Type "why", and repeat it.

easter eggs why

 4. Type "spy"

easter eggs spy

5. Type "penny"

easter eggs penny

6. Type "xpbombs"

easter eggs xpbombs

7. Type "fifteen"

easter eggs fifteen
 8. Type "step"
9. Type "imagesAndVideo"


10. Type "surf(membrane)"


11. Type "logo"

12. Type "imagesc(hot)"
13. Type "cruller"
14. Type "census"
15. Type "earthmap"
16. Type "eigshow"


17. Type "imageext"


18. Type "knot"
19. Type "lorenz"


20. Type "makevase"


21. Type "truss"


22. Type "vibes"


23. Type "wrldtrv"


24. Type "xpquad"


25. Type "xpsound"


26. Type "eml_asteroids"


27. Type "eml_fire"


28. Type "sf_tictacflow"


29. Type "rlc_gui"


30. Type
        load handel
    sound(y, Fs)

31. Type "toilet" (works in matlab older versions)
32. Type "shower" (works in matlab older versions)
33. Type "lala" and the "lalala" (works in matlab older versions)
34. Type "viper" (works in matlab older versions)
35. Type "tetris_2" (works in matlab older versions)

* Above commands except 31,32,33,34,35 are tested on Matlab R2013a.

Thursday, December 4, 2014

Sorting algorithms

The main purpose of a sorting algorithm is to arrange the elements of a list or an array in a certain order. In this post I am going to discuss 11 sorting algorithms described in Wikipedia.
  1. Insertion sort
  2. Selection sort
  3. Merge sort
  4. Heap sort
  5. Quick sort
  6. Bubble sort
  7. Shell sort
  8. Comb sort
  9. Counting sort
  10. Bucket sort
  11. Radix sort

Sort an array with 1s and 0s.

Sometimes you may need to sort an array consisting only two numbers. Let say ones and zeros. Here I am going to explain an algorithm, to solve such problem using one traverse, without using an additional array, and what really happen is, that it segregates ones and zeros in the array. You may see that this method is efficient than a usual sorting algorithm.

Let’s take the following array as an example. 

 array = {0, 1, 1, 0, 0, 1, 1} 

Here I use two variables named left and right.
  •  left – keep the array index when it is moved from left to right (left++)
  •  right - keep the array index when it is moved from right to left (right--)
The variable left increases until array[left] equals to 0 and similarly variable right decreases until array[right] equals to 1. If the left is less than right then 0 and 1 get interchanged and similar process happens until left is less than right.
  1. {0, 1, 1, 0, 0, 1, 1}    left = 0, right = 6;
  2. {0, 1, 1, 0, 0, 1, 1}    left = 1, right = 6;
  3. {0, 1, 1, 0, 0, 1, 1}    left = 1, right = 5;
  4. {0, 1, 1, 0, 0, 1, 1}    left = 1, right = 4;
  5. {0, 0, 1, 0, 1, 1, 1}    left = 1, right = 4;
  6. {0, 0, 1, 0, 1, 1, 1}    left = 2, right = 4;
  7. {0, 0, 1, 0, 1, 1, 1}    left = 2, right = 3;
  8. {0, 0, 0, 1, 1, 1, 1}    left = 2, right = 3;
  9. {0, 0, 0, 1, 1, 1, 1}    left = 3, right = 3;
  10. {0, 0, 0, 1, 1, 1, 1}    left = 3, right = 2;
Here is the C implementation of the above algorithm.

void sort_zeros_ones(int array[], int size)
{
  int left = 0, right = size-1;   
  while(left < right)
  {
     while(array[left] == 0
&& left < right)
        left++;
     while(array[right] == 1 && left < right)
        right--;
     if(left < right)
     {
       array[left] = 0;
       array[right] = 1;
       left++;
       right--;
     }
  }
}