Data Sorting in MATLAB

Data sorting techniques are often used in data processing programs. MATLAB provides a special function that is 'sort' to do sorting. Using 'sort' can be in two ways. The first way is used to sort the data in the column, its syntax as follows:

var2 = sort(var1,1)

var1 is the matrix or vector to be sorted. Here's how to use it in the program:
>> a=[2 5 7; 7 5 1; 8 7 5]
a =

     2     5     7
     7     5     1
     8     7     5

>> b = sort(a,1)
b =

     2     5     1
     7     5     5
     8     7     7

The second way is used to sort the data on the direction of the line, its syntax as follows:
 >> a=[2 5 7; 7 5 1; 8 7 5]
a =

     2     5     7
     7     5     1
     8     7     5

>> b = sort(a,2)
b =

     2     5     7
     1     5     7
     5     7     8

Data Orientation and Augmentation

Changing the data and put data is very commonly used in the program.. In the other programming language it may be quite difficult. But of course in MATLAB it becomes very easy.

-Changing the orientation of the data by the transpose
>> x = [1 3 5; 2 4 6]
x =

1     3     5
2     4     6

>> x = x'
x =

1     2
3     4
5     6

-Put data on the line.
>> x = [1 3 5;2 4 6]
x =

     1     3     5
     2     4     6

>> y = [7 7 7]
y =
    7     7     7

>> aug = [x;y]
aug =
     1     3     5
     2     4     6
     7     7     7

-Put data on the Column
>> x = [1 3 5; 2 4 6]
x =
     1     3     5
     2     4     6

>> y = [5;5]
y =
    5
    5

>> aug = [x y]
aug =
    1    3    5   5
    2    4    6   5

How to Build Data in MATLAB

In some cases common programs use the initials of data in the form of a matrix or array, such as zero matrix, identity matrix and others. Simply MATLAB provides several techniques to build data quickly, as follows:

-Building data-elements that have been determined.
For example you will build the data x with a known value, then the way of writing as follows:
i. row vector data:
>> x = [1 3 5]
x =
     1 3 5

ii. column vector data
 >> x=[1;3;5]
x =
    1
    3
    5

iii. for the data matrix form
 >> x = [1 3 5;2 4 6]
x =
    1   3   5
    2   4   6

-Build data with initial boundary and final boundary.
Suppose you want to create a data point from the point of 10 degrees to 15 degrees, then the way the writing is as follows:
>> ang = [10:15]
ang =
    10 11 12 13 14 15

-Establish initial boundary data, increment and final boundary
Suppose you want to create a data point from the point of 10 to 50 degrees with a 10 increase, then the way its writing is as follows:
 >> ang = [10:10:50]
ang =
     10   20   30    40    50

-Establish initial boundary data and the deadline, but the amount of data specified
Suppose you want to make 5 pieces of data in the interval angle angle 30 degrees to 70 degrees, then the way his writing is as follows:
 >> ang = linspace(30,70,5)
ang =
     30  40  50  60  70

-Building a logarithmic data with initial boundary and final boundary, but the amount of data specified.
Suppose you want to create 5 data values within the interval 10 and 100, then the way is as follows:
>> y = logspace(1,2,5)
y =
    10.0000   17.7828   31.6228   56.2341  100.0000

-Establish data using standard MATLAB matrix
How to create a data matrix with all elements of value 1:
 >> x = ones(2,3)
x =
    1   1   1
    1   1   1

Create a data matrix with all elements of value 0;
>> x  =  zeros(2,3)
x =
    0   0   0
    0   0   0

Making identity matrix of data, do the following:
 >> x  =  eye(2,2)
x =
   1   0
   0   1

-Building Data-Random
Random data is very often used in programming, particularly the field of mathematical modeling. MATLAB provides a quick way to generate random data as follows:
>> x  =  rand(3,3)
x =
    0.9501    0.4860    0.4565
    0.2311    0.8913    0.0185
    0.6068    0.7621    0.8214

Seen that the generated random data is in the interval 0 and 1. Then how to generate random data with other intervals, such as intervals 2 and 4?

Here is syntax to generate data with random intervals:
variable = (end-(rand () * (end-start)))

Example of usage in the program are as follows:
>> x  =  (4-(rand(3)*(7-5)))
x =

    3.1106    2.1564    3.1886
    2.7691    2.5236    2.1291
    2.4161    3.6475    2.1662

Looping and Conditional-Control Flow : Absolute value | switch. .. case ... otherwise ... end

Absolute value of the conditional (switch. .. case ... otherwise ... end)
This conditional syntax can only be used for conditions with a value that is not within certain intervals, can be either numeric or string. Command syntax-case switches are as follows:

switch variable
case value1
   command
case value2
   command
.
.
.
otherwise
   command
end

For clarity, the following are examples of its use in the program:
1. In the command window, type:
>> edit

2. Press Enter, then came MATLAB Editor and you type the program under the following:
clear all;
clc

disp ('--------------------------');
disp ( 'My 7th Training Program');
disp ('--------------------------');

disp ( 'options calculation formula');
disp ('1. cube surface area');
disp ('2. Volume of the cube ');
disp ('3. Ball surface area');
disp ('4. Volume of the Ball ');
disp ( '');
select = input ( 'Your choice (1-4) ->');

switches select
case 1
disp ( 'Calculate the area of the cube');
disp ('------------------');
length = input ( 'length = box');
area = length ^ 6 * 2;
disp ([ 'cube surface area =' num2str (area)]);

case 2
disp ( 'Calculate the Volume cube');
disp ('------------------');
length = input ( 'length = box');
volume = length ^ 3;
disp ([ 'volume of the cube =' num2str (volume)]);

case 3
disp ( 'Calculate the area of the ball');
disp ('------------------');
radius = input ( 'radius of ball =');
area = 4 * pi * radius ^ 2;
disp ([ 'ball surface area  =' num2str (area)]);

case 4
disp ( 'Calculate the volume of the ball');
disp ('------------------');
radius = input ( 'radius of ball =');
volume = 4 * pi * radius ^ 3 / 3;
disp ([ 'Volume of the ball =' num2str (volume)]);
otherwise
disp ( 'Your choice inconsequential !!!');
end;

3. When finished typing the above programs, you save in the directory c: / mytraining, with the name training07.m

4. Ensure your file storage directory is contained in the directory search list MATLAB. Then type the file name without the extension training06:
>> training07

5. Press Enter, then the program will run and produce as follows:
--------------------------
My 7th Training Program
--------------------------
Calculation formula options
1. cube surface area
2. Volume of the cube
3. Ball surface area
4. Volume of the Ball

Your  choice (1-4) -> 2
Calculate the volume cube
------------------
long box = 3
volume of the cube = 27

6. Done

Looping and Conditional-Control Flow : Conditional/Branches

Take a look also at the previous article: Looping and Conditional-Control Flow : Iteration conditioned

Control Flow # 2: Conditional / Branches
Conditional is a useful control to turn the program into a specific process. Usually used to complete the program that has many processes, but in one occasion execution only run one or more selection process based on certain conditions

Conditional relative value (if. .. elseif ... else ... end)
This conditional syntax can be used for conditions that are in a certain interval value or absolute, either numeric or string. So that control flow is the most common type used by the programmer. The way the writing is as follows:

if condition
  command
elseif
  command
else
  command
end

For clarity, the following are examples of its use in the program:
1. In the Command Window, type:
>> edit

2. Press Enter, then came MATLAB Editor and you type the program under the following:

clear all;
clc;

disp('--------------------------');
disp('My 6th Training Program');
disp('--------------------------');

test1 = input ( 'value test1 =');
test2 = input ( 'value utest2 =');
test3 = input ( 'value test3 =');

na = (test1 * 20/100) + (test2 * 30/100) + (test3 * 50/100);

disp ( '[final value =' num2str (na)]);

if na> 80
   disp ( 'Your grade = A');
elseif na <= 80 & na> 70
   disp ( 'Your grade = B');
elseif na <= 70 & na> 60
   disp ( 'Your grade = C');
elseif na <= 60 & na> 50
   disp ( 'grade you = D');
else
   disp ( 'Your grade = E');
end;

3. When finished typing the above programs, you save in the directory c: / mytraining, with the name

training06.m

4. Ensure your file storage directory is contained in the directory search list MATLAB. Learn it in the MATLAB directory management here.Then type the file name without the extension training06:
>> training06

5. Press Enter, then the program will run and produce as follows:
 --------------------------
My 6th Training Program
--------------------------
test1 value = 60
test2 value = 80
test3 value = 50
Your grade = C
>>

6. Done

Looping and Conditional-Control Flow : Iteration conditioned

Take a look also at the previous article: Looping and Conditional-Control Flow : Repetition / Iteration / Looping 

Iteration conditioned (While. .. end).
This syntax is used to perform the process without a known repetition of repetition. Iterations of this type of repetition only stop when it reaches certain conditions. Eg calculate the amount of glass needed to accommodate the contents of the tub. The way the writing is as
follows:

while conditions
commands
end

For clarity, the following are examples of its use in the program:
1. In the command window, type:
>> Edit

2. Press Enter, then when MATLAB editor appear, type or copy the program below:
clear all;
clc;

disp ('--------------------------');
disp ( 'My 5th Training Program');
disp ('--------------------------');

vbasin = input ( 'volume of the tub (ltr) =');
vglass = input ( 'glass volume (ltr) =');

nglass = 0;while vbasin> 0nglass = nglass +1;
vbasin = vbasin - vglass;
end;

disp ([ 'glasses required =' num2str (nglass)]);

3. When finished typing the above programs, you save in the directory c: / mytraining, with the name training05.m

4. Ensure your file storage directory is contained in the directory search list MATLAB. Learn it in the MATLAB directory management  here. Then type the file name without the extension training04:
>> training05

5. Press Enter, then the program will run and produce as follows:
--------------------------
My 5th Training Program
--------------------------
bath volume (ltr) = 2
glass volume (ltr) = 0.2
glass required = 11

6. Done