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

Looping and Conditional-Control Flow : Repetition / Iteration / Looping

In making a more complex program, MATLAB has a syntax for regulate the flow of the program. Program flow controller / control flow consists of 2 types : repetition and conditional.

Control Flow # 1: Repetition / Iteration / Looping

Repetition is the type of controller that allows you to streamline the writing script programs, especially those for programs that require the repeatedly. Repetition is often also referred to as iteration or looping.
Henceforth we only use the term iteration.

Limited iteration (for. .. end)
This iteration syntax is used to perform a repetition of the process has known quantity. For example to calculate the factorial 5, it clearly known number of iterations is 5. The way the writing is as follows:

For variable = start: interval: finish
                commands
end

For more details, here is an example of its use in the program as follows:

1. In the command window, type:
>> Edit

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

clear all;
clc;

Disp ('--------------------------');
Disp ( 'My 4th Training Program');
Disp ('--------------------------');

data = input ( 'limit of iterations =');
for n = 1: data
for m = data: -1:1
x (n, m) = n ^ 2 + (5 * m)
end;
end;

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

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

5. Press Enter, then the program will run and produce as follows:

--------------------------
My 4th Training Program
--------------------------
iteration limit = 1

x =

6 11 16
9 14 19
14 19 24

6. Done

In the next article we'll  still talk about another form of iteration but it is for unknown quantity.

Input and output dynamic program

If your program before was not interactive, not dynamic, and rigid impressed because every time you want to use the new data you should change its data in the script, you can just make it dynamic.

MATLAB provides the facility to be able to interact directly with the program without having to change the script. To ask for input from the user, MATLAB provides input function. Syntax of writing as follows:

variable = input ( 'string display');

and to display program output to the screen, MATLAB provides the function of Disp. Syntax of writing as follows:

Disp ( 'string is displayed')

Here is an example of writing to receive input syntax is continued by displaying the results of the program to the screen:

1. In the command window, type:

>> Edit

2. Hit enter, then appeared MATLAB Editor and you type in the program follows:

clear all;
CLC;

Disp ('---------------------------');
Disp ( 'My 3th Training Program');
Disp ('---------------------------');

length = input ( 'length of data =');
width = input ( 'width data =');
area = length * width;
Disp ([ 'Area ->', num2str (area)]);

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

4. Ensure your file storage directory is contained in the list MATLAB directory search. You can learn how to do that in directory management article.  Then type the file name without the extension training03:

>> training03

5. Press Enter, then the program will run, in succession enter variable length and width and then hit enter and then hit enter and produce output as follows:

--------------------------
My 3th Training Program
--------------------------
length of data = 6
Data width = 2
Area -> 12
>>

6. Done

* Note:
At the end of the program code is num2str code. Num2str code serves to convert the data type of the original area of numerical type into string type. This is in order to be placed together with another string in the mark these brackets.

General Mathematical Function in MATLAB

Common mathematical functions are often used and provided in the literature MATLAB function is as follows:

Here is an example of using trigonometric functions:

Trigonometry Functions
Description

acos
inverse cosine

acosh
inverse hyperbolic cosine

acot
inverse cotangen

acoth
inverse hyperbolic cotangen

acsc
inverse cosecan

acsch
inverse hyperbolic cosecan

asec

inverse secan

asech
inverse hyperbolic bag

salty
inverse sine

asinh
inverse hyperbolic sine

atan
inverse tangent

atanh
inverse hyperbolic tangent

cos
cosine

cosh
hyperbolic cosine

cot
cotangen

coth
hyperbolic cotangen

csc

cosecan

csch

hyperbolic cosecan

sec
secan

sech
hyperbolic secan

sin
sine

sinh
hyperbolic sine

tan
tangent

tanh
hyperbolic tangent





1. In the command window, type:
>> Edit

2.Press enter, then appeared MATLAB Editor and you type in the following programs :

clear all '
CLC;

x = [0:10:180];% generate the data point

y1 = sin (x * pi/180);% calculation data sine x
y2 = cos (x * pi/180);% calculating the cosine of data x

out = [x 'y1' y2 ']

3. After finish typing or copying  program above, save it in the directory c: / mytraining, gide the name 'training02.m'

4. Ensure your file storage directory is contained in the list MATLAB directory search. You can learn how to do that in directory management article. Then type the name of the file training02 twithout extension:
>> training02

5. Press Enter, then the program will run and produce output in the window of the data x, y1, and y2

Besides MATLAB also provides other mathematical functions such as

Exponential Function 
Description
exp
Exponential
log
natural logarithm
log10
logarithm base10
log2
logarithm base 2
sqrt

root

If see table functions above , you may be confused, how to use that functions? The easiest method to find out how to use the function is as follows:

1. Suppose you want to know how to use MATLAB functions to log10, type the following at the command window:

>> help log10

2.Press enter, then you will get the information you want as follows:

Common log10 (base 10) logarithm.
Log10 (X) is the base 10 logarithm of the elements of X.
Complex results are produced if X is not positive.

See also LOG, log 2, EXP, LOGM.

3. So how to use it is as follows:
>> X = 100;
>> Y = log10 (x)
b =
       2

Complex Number Operation in Matlab programming

Another advantage of the Matlab programming is the ability to process the data complex numbers without requiring special variable declarations. Here is to declare a variable to the complex numbers:

>> a=2+1.5i
a =
   2.0000 + 1.5000i
>> b=3-4j
b =
   3.0000 -4.0000i

In conclusion, there is no difference using the identifier 'i' or 'j' for the number complex. For the purposes of mathematical calculation it does not take special functions, for example, as follows:

>> a + b
ans =
    5.0000 -2.5000i

As for the separation needs of real and imaginary values can easily done, for example by the following examples:

>>a=2+1.5000i
a= 2.000 + 1.5000i
>> real(a)
ans =
      2
>>imag(a)
ans =
     1.5000

Complex form a + bi in the complex integer arithmetic rectangular shape, whereas the polar form of complex numbers is realized with Magnitude and angle. Conversion from rectangular to polar form in Matlab met through the functions abs and angle.

>> a= 2+1.5i
a =
   2.0000 + 1.5000i
>>abs(a)
ans =
    2.5000
>> angle(a)
ans =
    0.6435

MATLAB Mathematic operation

Mathematical operation in matlab programming is very simple, it similar if you use the regular calculator. Here is mathematic table operator that use in Matlab programming.


Addition
+
A + B
Subtraction
-
A - B
Multiplication
*
A * B
Division
/ or \
A / B or A \ B
exponent
^
A ^ B

Knowledge of the matrix is essential in Matlab programming because all patterns of mathematical operations will be restored in the pattern matrix math operations. For example is when we declare variable 'a' and fill it with the value 5 in the following way :
>> a=5
a =
    5

otomatically, matlab will recognize variable 'a' above as a matrix which has 1x1 matric dimension. It can be proof as follow :
>> a(1,1)
ans =
      5

The difference will be more felt when we do an operation that entangle multiplication and division. For example we can use the case of area calculation whith length and width data is provided. As a first case, it is provided length data (5) and width (6). the solution for this case as follow :
>> length = 5;
>> width = 6;
>> area = length*width;
area = 30

whereas  for second case, it is provided four datas in each variable, length (4,5,3,1) and width (5,3,2,2), if we use same method, it will result error message :
>> length = [4 5 3 1]
length =
        4 5 3 1
>> width = [5 3 2 2]
width =
        5 3 2 2
>> area = length*width
??? Error using ==> *
Inner matrix dimensions must agree.

An error message above is shown because it is not meet the matrix multiplication requirement. You should remember that first column of the first matrix should be same with total row of second matrix.

How to Access Variable in MATLAB

How to access variable in Matlab?By default, MATLAB identify variable which you use as a matrix or array. So for variable which contain more than one element, addressing each element variable element in MATLAB following the following notation:
variable(line, column)

For example you can define in command window a matrix which has 3x3 dimension:
>> a = [1 3 5; 5 6 7; 8 2 4]
a =
     1 3 5
     5 6 7
     8 2 4

To access single element of matrix above as follow :
>> a(3,2)
ans =
         2

It means that you are accessing matrix element of 'a' variable in third line and second column.

To access element in the certain line as follow:
>> a(3,:)
ans =
         8 2 4
It means that you are accessing matrix elements of 'a' variable in third line. Sign double dot (:) at the matrix column means all column.

To access element in the certain column as follow:
>> a(:,3)
ans =
         5
         7
         4

It means that you are accessing matrix element of 'a' variable in the third column. Sign double dot (:) in the line means all line

To access some elements in certain line and column as follow:
>> a(1:2,2:3)
ans =
        3 5
        6 7

It means that you are accessing matrix element of 'a' variable in first to second line and in second to third column. Writing (1:2) at the line means first to second line. Writing (2:3) at the column means second to third column.