Sie sind auf Seite 1von 31

Introduction to Computation Matlab Module

Tim Materi Pengenalan Komputasi 2019/2020

2019-11-09

1
Notes

1. This module is specifically made to guide Introduction to Computation programming.


Therefore, some materials are not included as they are away from Introduction to Com-
putation scope.
2. You may open this module during the practical exercise session.

3. You are recommended to run the programs within the module in your computer, to know
the output of the program.
4. It is more preferable to try the programs listed in this module to grasp deeper on what the
program does.

5. You are highly advised to learn programming from other sources or materials and explore
further about the programming language you use.

2
Contents
1 Module 1 4
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.1 Arithmetic Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.2 Operator Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.3 Relational Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.4 Logical Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Module 2 8
2.1 Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.1 While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.2 For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Nested looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Module 3 10
3.1 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.1.1 Array Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.1.2 Array and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Module 4 12
4.1 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.2 Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.3 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5 Module 5 15
5.1 Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.1 Creating Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.2 Reading and Writing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.3 Data Accessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.4 Extracting Extreme Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.1.5 Data Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.1.6 Frequency Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.1.7 Determining Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.1.8 Simple Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.1.9 Correlation Coefficient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.1.10 Data Unstacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.2 Data Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2.1 Bar Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2.2 Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2.3 Pie Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.2.4 Stacked Bar Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.2.5 Line Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.2.6 Area Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.2.7 Scatter and Bubble Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3
1 Module 1

1.1 Introduction

In this module, we are using Matlab that is compatible with version 9.1 or later. Matlab IDE is
paid, therefore you are recommended to learn Octave (a programming language, compatible and
free alternative to Matlab). You can try Octave Online on https://octave-online.net/

Characteristics of this programming language:

• Matlab is case-sensitive, which means small letters and capital letters have different inter-
pretation.
• Matlab does not detect indent and line changes. However, it is highly advised to properly
align indent as it is handy for debugging and code readability.
• Variables in Matlab are implicit and dynamics. In other words, a variable does not need to
be defined by the data type. Yet in this module, we still have to learn the existing data type.

1.2 Input and Output

In Matlab, the following program writes out "Hello, World!" to the screen:

fprintf (’Hello , World !\n’); % (1)

fprintf (’This is my’); % (2)


fprintf (’ first program \n’); % (3)

% This is a comment

% Anything after percentage sign ( % )


% will be ignored by the compiler .

%{
Apart from that , everything between percentage sign followed by open curly
bracket and percentage followed by closed curly bracket are ignored too. Make
sure there are no texts in the same lines as the percent and curly bracket .
%}

The part marked as 1 writes out ’Hello, World!’ to the screen. The syntax ’\n’ will move line
after writing them. In other words, the output of the program above is ’Hello, World!’ followed
by ’This is my next program’ in the next like.
1
Apart from that, note that each statement ends with a semicolon (;).

To make an input, we need something to store the data inputted. For example, the following
program receives input numbers and rewrite the numbers entered.

S = input(’Input some text : ’, ’s’); % 1


fprintf (’You wrote %s\n’, S); % 2

N = input(’Input a number : ’); % 3


1 Matlab does not require you to use, are only used to hide output https://www.mathworks.com/help/matlab/matlab_

prog/matlab-operators-and-special-characters.html

4
fprintf (’You input a text %s and a number %d\n’, S, N + 5); % 4

On number (1) and (3), program reads input from the user and entered to the variable S and N.
Noticed that to read a string string (a set of character), use ’s’ as a second parameter for input.
Apart from int, string, Matlab gets to receive float (real number) data, and etc.

1.3 Data Types

There are several types of data, but in this course we will only use these data types often:

bool Boolean True or False


int Integer all integers
float Real number all real numbers
string Text a set of characters

Contoh penggunaan:

B = true; % Boolean
I = 12345; % Integer
R = 2.331973; % Real number
S = ’def ’; % Text

1.4 Operator

1.4.1 Arithmetic Operator

Operator Description Example


+ Addition 2 + 3 gives 5
- Subtraction 1 - 8 gives -7
* Multiplication 5 * 6 gives 30
/ Division 13 / 5 gives 2.6
mod Modulo mod(13, 5) gives 3

1.4.2 Operator Assignment

Operator Deskripsi Example


= Assignment N = 5
+= Addition N += 5, N akan ditambah 5.
-= Subtraction N -= 5, N akan dikurang 5.
*= Multiplication N *= 5, N akan dikali 5.
/= Division N /= 5, N akan dibagi 5.

5
1.4.3 Relational Operator

Operator Description Example if True Example if False


== Equals to 2 == 2 2 == 3
˜= Not equals to 3 != 2 3 != 3
< Less than 2 < 3 2 < 2
> More than 3 > 2 3 > 2
<= Less than or equal to 2 <= 2 1 <= 3
>= More than or equal to 6 >= 5 2 >= 4

1.4.4 Logical Operator

Operator Description Example if True Example if False


&& And (1 < 2)&& (3 == 3) (1 == 2)&& (3 == 3)
|| Or (1 < 2)|| (4 == 3) (3 < 2)|| (2 == 3)
˜ Negate ˜(3 < 2) ˜(1 > 2)

1.5 Control

We can control the flow of our program in programming. Thus, our program behaves depends
on users’ input. Suppose, we make a program that checks if a number is positive:

N = input(’Masukkan nilai N: ’);

if (N > 0)
fprintf (’%d is a positive number ’, N)
end

Then, if we want to handle the non-positive number:

...
if (N > 0)
fprintf (’%d is a positive number ’, N)
else % N <= 0
fprintf (’%d is not a positive number ’, N)
end
...

However, it is obvious that numbers can be zero or negative, so we add:

...
if (N > 0)
fprintf (’%d is a positive number ’, N)
elseif (N < 0)
fprintf (’%d is a negative numbe ’, N)
else % N == 0
fprintf (’%d is zero ’, N)
end
...

Notice that we can repeat the else operation. Apart from that, we can place if inside of if.

6
...
if (N >= 0)
if (N > 0)
fprintf (’%d is a positive number ’, N)
else % N == 0
fprintf (’%d is zero ’, N)
endif
else % N < 0
fprintf (’%d is a negative number ’, N)
end
...

7
2 Module 2

2.1 Looping

In programming, repetitive processing is required often to achieve a certain result. If this repetition
is done manually, the source file size would be too large. A simple example that writes ”Hello
World” on the screen 1000 times would require at least 1000 command lines. By using repetition
syntax, this problem can be solved using only a program of a few lines.

for i = 1:1000
fprintf (" Hello world\n");
end

2.1.1 While Loop

One of the looping syntax that is frequently used is While-Do syntax. The program checks if a
condition given is fulfilled before running the statement in it.

The following is a program that receives a and b, and then writes out a, a + 1, a + 2, . . . , b − 1, b.

a = input(’’);
b = input(’’);
i = a;
while i <= b
fprintf (’%d\n’, i);
i = i + 1;
end

2.1.2 For Loop

The other form of looping syntax is for. This syntax is usually used for repetition parameters are
known. For loops can also be made with a while-do loop.

The following is a program that receives a and b, and then writes out a, a + 1, a + 2, . . . , b − 1, b.

a = input(’’);
b = input(’’);
for i = a:b
fprintf (’%d\n’, i)
end

The range can have two or three parameters. For more details, consider the following code part:

2 : 5 % 2, 3, 4, 5
7 : -1 : 2 % 7, 6, 5, 4, 3, 2
3 : 0.5 : 5 % 3, 3.5, 4, 4.5, 5

8
2.2 Nested looping

Furthermore, looping can be assigned inside of looping, known as nested looping. For example,
a program that forms square patterns.

n = input(’’);
for i = 1:n
for j = 1:n
fprintf (’*’);
end
end

9
3 Module 3

3.1 Array

Array is a variable of a single name, but contain multiple values. Each element in an array can be
accessed through index.

Consider the following example:

Index 1 2 3 4 5 6 7 8 9 10
A 10 5 7 11 19 23 35 37 12 1

• A(10)= 1
• A(1) = 10
• A(7) = 35

In the example above, we have a variable named A, contains 10 values, where each value can be
accessed through index. To access the xth index, use A(x). We can assume the value A(x) as a
stand-alone variable. This concept is what we call an array. Note that indexing in Matlab starts at
1, differs from the other programming languages, that use zero-based indexing.

Extras: To receive an array of string, use in the indexing, as follows:

A{1}= input(’Masukkan nama: ’,’s’)

3.1.1 Array Declaration

Since array is also a variable, it requires declarations like other variables.

Example of declaration:

• X = zeros(1, n); creates an array with a size of n with its content 0.


• X = ones(1, 100); creates an array with a size of 100 with its content 1.
• X = cell(5, 1); creates an empty cell array that can be used for an array of strings, with a
size of 5.

In Matlab, the array used is essentially a matrix with a size of one by the length defined. The
array A defined in the example above, are A(1), A(2), . . . , A(10). For a cell array, it can be accessed
using a curly bracket A{1}. Accessing the index value beyond the limit bound will cause the array
to stretch to satisfy the index that is replacing.

3.1.2 Array and Variables

Just assume them as a variable so that every operation types on variables also apply to arrays. For
example, the following array:

Table = zeros(1, 10);

10
Therefore the following table array are defined for index #1 up to #10. Then we can make an
operation of an element.

Table(2)= input(’’);

If we were given 5 elements, and we need to save the element on the table, with:

Table (1) = input(’Input value #1: ’);


Table (2) = input(’Input value #2: ’);
Table (3) = input(’Input value #3: ’);
Table (4) = input(’Input value #4: ’);
Table (5) = input(’Input value #5: ’);

However, the input method above is laborious, especially for a large array. It would be more
efficient if we input using looping.

Tabel = zeros (1, 5);


for i = 1:5
fprintf (’Input value #%d: ’, i);
Table(i) = input(’’);
end

11
4 Module 4

4.1 Function

What is a function? A function is part of a program that can work on certain tasks or operations
outside the main program. The function returns value according to the algorithms given in the
function.

Example: A simple function to determine squared of a number:

function x2 = square (x)


x2 = x*x;
end

The function above is named as square and returns the squared value of x. The output variable
must be written before the function name, followed by an equal symbol. The output variable
returns when the function is invoked on the main program.

Example: A complete program that receives input and outputs, the input number squared.

n = input(’Masukkan n: ’); % Integer input from keyboard


n2 = square (n); % Invoke the function
fprintf (’%d’, n2); % Printing output

function x2 = square (x)


x2 = x*x;
end

From the example above, the variable in the main program is n. Whereas in function, the variable
is named as x. Even though x changes, the values of n in the main program will not change.

A function can receive more than one input parameter. In addition to that, functions can do specific
tasks like a normal program does, but still unable to change variables in the main program.

Example: A function that calculates ab :

function c = pangkat (a, b)


c = 1; % initiation
for i = 1:b
c = c * a; % multiplication
end
end

Note: You are not recommended to use array(s) as function parameters, as these are not taught in
Introduction to Computation Course.

4.2 Procedure

Procedures are similar to functions, but with no return value, despite the syntax named as function.
For example, a program that writes out menus:

fprintf (’Menu :\n’);

12
fprintf (’1. Burger \n’);
fprintf (’2. Friend Chicken \n’);
fprintf (’3. Instant Noodle \n’);
food_option = input(’Masukkan pilihan : ’);

fprintf (’Menu :\n’);


fprintf (’1. Avocado Juide\n’);
fprintf (’2. Thai Tea\n’);
fprintf (’3. Milk Tea\n’);
beverage_option = input(’Masukkan pilihan : ’);

fprintf (’Menu :\n’);


fprintf (’1. French Fries\n’);
fprintf (’2. Kerupuk \n’);
fprintf (’3. Doughnut \n’);
additional_option = input(’Masukkan pilihan : ’);

Like in the program above, writing menus multiple times can be summarized to:

tulis_menu (’Burger ’, ’Fried Chicken ’, ’Instant Noodle ’);


pilihan_makanan = input(’Input choice : ’);

tulis_menu (’Avocado Juice ’, ’Thai Tea ’, ’Milk Tea ’);


pilihan_minuman = input(’Input choice : ’);

tulis_menu (’French Fries ’, ’Kerupuk ’, ’Doughnut ’);


pilihan_tambahan = input(’Input choice : ’);

function tulis_menu (pil1 , pil2 , pil3)


fprintf (’Menu :\n’);
fprintf (’1. %s\n’, pil1);
fprintf (’2. %s\n’, pil2);
fprintf (’3. %s\n’, pil3);
end

4.3 Matrix

Matrix is essentially 2 dimensional array, it can be declared with the following syntax:

A = zeros (10 ,20);

From the code above, a matrix named A is declared with element type integer and initial value of
0, with a size of 10 rows × 20 columns. Similar to array, the i-th row and j-th column are accessed
with A(i,j) syntax.

Example: A program that reads each element in a matrix and rewrite each element to the console.

n = input(’’);
m = input(’’);

A = zeros(n, m);

for i = 1:n
for j = 1:m

13
fprintf (’masukkan elemen baris %d kolom %d: ’, i, j);
A(i,j) = input(’’);
end
end

for i = 1:n
for j = 1:m
fprintf (’%d ’, A(i,j));
end
fprintf (’\n’);
end

The following is an example of a program that reads matrix A with a size of n × m, matrix B with
a size of n × l.

n = input(’’);
m = input(’’);
l = input(’’);

A = zeros(n, m);
B = zeros(m, l);
C = zeros(n, l);

for i = 1:n
for j = 1:m
fprintf (’masukkan elemen B baris %d kolom %d’, i, j);
A(i, j) = input(’’);
end
end

for i = 1:m
for j = 1:l
fprintf (’masukkan elemen B baris %d kolom %d’, i, j);
B(i, j) = input(’’);
end
end

for i = 1:n
for j = 1:l
C(i, j) = 0;
for k = 1:m
C(i, j) = C(i, j) + A(i, k)*B(k, j);
end
fprintf (’%d ’, C(i, j));
end
fprintf (’\n’);
end

14
5 Module 5

In this module, the data used in can be downloaded on https://drive.google.com/drive/


folders/1o2Zg_Lc911dsW0Iw37uWgYqM0-dR8Jro?usp=sharing.

5.1 Data Analysis

5.1.1 Creating Table

% creating table with column ’name ’ and ’age ’


name = {’ani ’;’budi ’;’caca ’;’dea ’;’egi ’};
age = [10;20;30;40;50];
T = table(name ,age);

Make sure that the name and age have the same quantity to make a table!

5.1.2 Reading and Writing Data

To read a data from .csv / excel workbook, we use readtable. To write the data to .csv or excel
workbook, for example:

% Reading a csv file ’tingkatinflasi20082013 .csv ’


% in the folder of a same file path with the Matlab code
M = readtable (’tingkatinflasi20082013 .csv ’);

% Reading an excel workbook at D:\


M = readtable (’D:/ data.xlsx ’);
% Reading an excel workbook at D:\, with a sheet name " Sheet1 "
M = readtable (’D:/ data.xlsx ’, ’Sheet ’, ’Sheet1 ’);

% Writing a .csv file in the same folder


writetable (M, ’data_out .csv ’);

% Writing an excel workbook in the same folder


writetable (M, ’data_out .xlsx ’);

% Writing an excel workbook in the same folder ,


% with a sheet name " Sheet1 "
writetable (M, ’data_out .xlsx ’, ’Sheet ’, ’Sheet1 ’);

Note: You are highly recommended to work on the same folder rather than using the format
D:/path/ke/file.xlsx.

5.1.3 Data Accessing

Here is the following example:

M = readtable (’tingkatinflasi20082013 .csv ’);

% extracting the 5th data

15
M(5 ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2009 ’Prov. Jawa Barat ’ 2.02

% extracting data number 5 to 7,


% column number 1 to 3
M(5:7 ,1:3)
% Tahun Cakupan Tingkat_Inflasi
% 2009 ’Prov. Jawa Barat ’ 2.02
% 2009 ’Nasional ’ 2.78
% 2010 ’Kota Bandung ’ 4.53

% extracting the 17th data to the end


M(17: end ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2013 ’Prov. Jawa Barat ’ 9.15
% 2013 ’Nasional ’ 8.38

% extracting the first 5 data


M(1:5 , :)
% Tahun Cakupan Tingkat_Inflasi
% 2008 ’Kota Bandung ’ 10.23
% 2008 ’Prov. Jawa Barat ’ 11.11
% 2008 ’Nasional ’ 11.06
% 2009 ’Kota Bandung ’ 2.11
% 2009 ’Prov. Jawa Barat ’ 2.02

% extracting the number of data


height (M) % 18

In addition, we can access data based on the criteria. Here is an example:

M = readtable (’tingkatinflasi20082013 .csv ’);

% extracting year 2012 data


cond1 = M.Tahun == 2012;
M( cond1 ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2012 ’Kota Bandung ’ 4.02
% 2012 ’Prov. Jawa Barat ’ 3.86
% 2012 ’Nasional ’ 4.3

% extracting data for ’Kota Bandung ’ prior to 2012


cond2 = M.Tahun < 2012 & isequal (M.Cakupan ,’Kota Bandung ’);
M( cond2 ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2008 ’Kota Bandung ’ 10.23
% 2009 ’Kota Bandung ’ 2.11
% 2010 ’Kota Bandung ’ 4.53
% 2011 ’Kota Bandung ’ 2.75

% extracting data with ’tingkat inflasi ’ higher than 10


% or lower than 3
cond3 = M. Tingkat_Inflasi > 10 | M. Tingkat_Inflasi < 3;
M( cond3 ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2008 ’Kota Bandung ’ 10.23
% 2008 ’Prov. Jawa Barat ’ 11.11
% 2008 ’Nasional ’ 11.06

16
% 2009 ’Kota Bandung ’ 2.11
% 2009 ’Prov. Jawa Barat ’ 2.02
% 2009 ’Nasional ’ 2.78
% 2011 ’Kota Bandung ’ 2.75

5.1.4 Extracting Extreme Values

Extreme values are the lowest or highest value in a certain data.

M = readtable (’tingkatinflasi20082013 .csv ’)

% extracting data with maximum inflation


[valmax , idxmax ] = max(M. Tingkat_Inflasi );
cond1 = M. Tingkat_Inflasi == valmax ;
M(cond1 ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2008 ’Prov. Jawa Barat ’ 11.11

% extracting data with minimum inflation


[valmin , idxmin ] = min(M. Tingkat_Inflasi );
cond2 = M. Tingkat_Inflasi == valmin ;
M(cond2 ,:)
% Tahun Cakupan Tingkat_Inflasi
% 2009 ’Prov. Jawa Barat ’ 2.02

5.1.5 Data Sorting

Data can be sorted ascending or descending.

M = readtable (’tingkatinflasi20082013 .csv ’)

% sorting data based on inflation level ( tingkat inflasi )


% ( column #3, ascending )
S = sortrows (M, 3, ’ascend ’)

% sorting data based on ’tahun ’ ascending (col 1),


% then ’tingkat inflasi ’ descending (col 3).
S = sortrows (M, [1 3], {’ascend ’ ’descend ’})

5.1.6 Frequency Table

We get to make a frequency table, based on the data on a selected column, meaning that it list of
the possible values in that column in a unique form, then count the tally of the ’unique’ data.

We are tabulating the frequency for the year 2008 to 2013. In Matlab, tabulation of integers or
floats displays value from 1 to 2007 too, which contains no tallies. This can be negated by adding
categorical to it.

M = readtable (’tingkatinflasi20082013 .csv ’)

17
% list of frequency tally of years (tahun) (kolom 1) on a data
tabulate ( categorical (M.Tahun))
% Value Count Percent
% 2008 3 16.67%
% 2009 3 16.67%
% 2010 3 16.67%
% 2011 3 16.67%
% 2012 3 16.67%
% 2013 3 16.67%

The table made above are for displaying purpose only, it cannot be process. Therefore, by adding
a variable name, it will return as a cell array, can hardly be used for visualizing, what we want to
do is convert the data to an actual table.

% list of frequency tally of years (tahun) (kolom 1) on a data


year_freq = tabulate ( categorical (M.Tahun))
year_freq = cell2table (year_freq ,’VariableNames ’,{’Value ’,’Count ’,’Percent ’});
% Value Count Percent
% ’2008’ 3 16.667
% ’2009’ 3 16.667
% ’2010’ 3 16.667
% ’2011’ 3 16.667
% ’2012’ 3 16.667
% ’2013’ 3 16.667

For cross-tabulation of frequency distribution between two distinct data variables, contingency
table, refer the stacked bar graph section.

5.1.7 Determining Range

M = readtable (’tingkatinflasi20082013 .csv ’)

% Extracting the maximum and minumum inflation rate.


[valmax , idxmax ] = max(M. Tingkat_Inflasi ); % 11.1100
[valmin , idxmin ] = min(M. Tingkat_Inflasi ); % 2.0200

fprintf (’The maximum inflation rate is %.4f’, valmax );


fprintf (’The minimum inflation rate is %.4f’, valmin );

5.1.8 Simple Statistics

M = readtable (’tingkatinflasi20082013 .csv ’)

summary (M)

We get to extract certain statistics:

M = readtable (’tingkatinflasi20082013 .csv ’)

% inflation rate average (col 3)

18
inflasi_avg = mean(M. Tingkat_Inflasi ) % 5.8189

% inflation rate standard deviation (col 3)


stdcol4 = std(M. Tingkat_Inflasi ) % 3.1487

5.1.9 Correlation Coefficient

Two selected columns may have correlation. These are measured with correlation coefficient, and
are interpreted as follows:

• The more it approaches 0, both columns correlates less.


• The more it approaches 1, the more both columns correlates directly (positive correlation).
• The more it approaches -1, the more both columns correlates inversely (negative correlation).

The following are example of a correlation between year and inflation level:

M = readtable (’tingkatinflasi20082013 .csv ’)

% correlation coefficient between tahun


% dan tingkat inflasi
corrcoef (M.Tahun , M. Tingkat_Inflasi )
% 1.0000 -0.1444
% -0.1444 1.0000
% Negatively correlation but not much
% It displays negative value but closer to zero.

5.1.10 Data Unstacking

Unstacking the stacked data to split the data by their unique entries. For instance, splitting the
variable cakupan (area scopes) into these separate variables.

unique (M. Cakupan ) % displaying the regions


% {’Kota Bandung ’ }
% {’Nasional ’ }
% {’Prov. Jawa Barat ’}

From the result above, there are 3 area scopes, which can be separated for each of them. The
output in unstacked format is:

M_cak = unstack (M,’Tingkat_Inflasi ’,’Cakupan ’)


% Tahun KotaBandung Nasional Prov_JawaBarat
% 2008 10.23 11.06 11.11
% 2009 2.11 2.78 2.02
% 2010 4.53 6.96 6.62
% 2011 2.75 3.79 3.1
% 2012 4.02 4.3 3.86
% 2013 7.97 8.38 9.15

19
Each row in the table above contains data from rows in the inflation data that have the same value
in the grouping variable, year. The order of the unique values in year determines the order of the
data in the table above.

However, the data for each group combination of year and region scope only have one data. For
a case of a combination of multiple data:

Tdata = readtable (’data.csv ’)

% Average number of pets per age per gender per state.


% Extracting gender (col3), state (col4), and num of pets (col 6)
% into another table , and sort by gender (1st col of the new table).
Tmean = Tdata (: ,[3 4 6]);
Tmean = sortrows (Tmean)
% gender state num_pets
% ’F’ ’DC ’ 1
% ’F’ ’TX ’ 2
% ’M’ ’CA ’ 0
% ’M’ ’CA ’ 2
% ’M’ ’CA ’ 5
% ’M’ ’DC ’ 5
% ’M’ ’TX ’ 3
Tmean = unstack (Tmean ,’num_pets ’,’state ’ ,...
’AggregationFunction ’,@mean)
% gender CA DC TX
% ’F’ NaN 1 2
% ’M’ 2.3333 5 3

The unstacked data and the aggregation function mean applied to multiple rows in the same group
that have the same values in the indicator variable. The only combination of data with more than
one data is the males from California (CA), with a total number of pets of 7 from three entries,
giving a mean of 2.3333.

The default aggregation function is sum, omitting the aggregation function parameter results:

Tdata = readtable (’data.csv ’)

% Total number of pets per age per gender per state.


% Extracting gender (col3), state (col4), and num of pets (col 6)
% into another table , and sorting them by gender .
Tsum = sortrows (Tdata (: ,[3 4 6]));
Tsum = sortrows (Tmean)
Tsum = unstack (Tmean ,’num_pets ’,’state ’)
% gender CA DC TX
% ’F’ NaN 1 2
% ’M’ 7 5 3

20
5.2 Data Visualization

Loading up data:

Tdata = readtable (’data.csv ’)


Tanimal = readtable (’animal .csv ’)
Tmedali = readtable (’medali .csv ’)

5.2.1 Bar Chart

% Vertical bar chart for age of an individual person


figure (1)
bar( Tdata.age); % create bar chart based on the selected table column
set(gca ,’xticklabel ’,Tdata.name) % set bar label corresponding to the row
grid on % turn on grid
xlabel (’Name ’); ylabel (’Age (years)’); % axis label
title (’Age of an individual person ’) % graph title

21
The bar graph can have multiple data represented by different colors. In this instance, we are
comparing the number of children Tdata.num_children and number of pets Tdata.num_pets in a
single bar graph.

% Number of children ( num_children ) dan pets ( num_pets ) in a


% single vertical bar chart
figure (2)
bar ([ Tdata. num_children ,Tdata. num_pets ]) % Don ’t forget the square bracket
set(gca ,’xticklabel ’,Tdata.name); grid on
xlabel (’Name ’); ylabel (’Age (years)’);
legend ({’children ’,’pets ’},’Location ’,’bestoutside ’)
title (’Number of children and pets per person ’)

Horizontal bar graph syntax are very similar to that of the conventional bar graph, but instead of
’xticklabel’, it uses ’yticklabel’ for labeling.

figure (3)
barh( Tdata.age);
set(gca ,’yticklabel ’,Tdata.name); grid on
xlabel (’Age (years)’); ylabel (’Name ’);
title (’Age of a person , but horizontal bar ’)

22
5.2.2 Histogram

% Histogram of a person age based on the group: 0 to 100 with a step of 20.
figure (4)
histogram (Tdata.age ,0:20:100) ; grid on
xlabel (’Age ’); ylabel (’Frequency ’)
title (’Individual person age grouped from 0 to 100 with a step of 20’)

23
Where, the parameters in this syntax are (data_column, ranges)

5.2.3 Pie Chart

% Composition of individuals based on states


% Creating Pie Chart
figure (5)
pie( categorical (Tdata.state))
title (’Composition of individuals based on states ’)

24
5.2.4 Stacked Bar Chart

Very similar to the grouped bar graph but stacked. In this instance, we are tabulating frequency
table between two distinct column in a data, which are gender (first order) and state (second
order).

% Contingency table , like in unstack section


tbl = sortrows (Tdata (: ,[3 4]))
tbl. tally = ones( height (tbl) ,1) % add tally column , full of ones
% gender state tally
% ’F’ ’DC ’ 1
% ’F’ ’TX ’ 1
% ’M’ ’CA ’ 1
% ’M’ ’CA ’ 1
% ’M’ ’CA ’ 1
% ’M’ ’DC ’ 1
tbl = unstack (tbl ,’tally ’,’state ’)
% gender CA DC TX
% ’F’ NaN 1 1
% ’M’ 3 1 1

From the result above, the variable state is unstacked into three, which are California (CA), the
DC, and Texas (TX). Aggregation function sum is applied, resulting a contingency table.

In the following code, in the bar graph syntax, curly bracket is used instead of normal bracket.
This ’convert’ table into double, to be plotted.

% Stacked bar graph of frequency of data as per gender per state


figure (6)
bar(tbl {: ,2: end},’stacked ’); grid on
set(gca ,’xticklabel ’,tbl. gender )
legend ( unique (Tdata.state), ’Location ’, ’bestoutside ’)
xlabel (’Gender ’); ylabel (’Frequency ’)
title (’Frequency of data as per gender per state ’)

25
% Number of children ( num_children ) dan pets ( num_pets ) in a
% single stacked bar chart
figure (7)
bar ([ Tdata. num_children ,Tdata. num_pets ],’stacked ’)
set(gca ,’xticklabel ’,Tdata.name); grid on
xlabel (’Name ’); ylabel (’Age (years)’);
title (’Number of children and pets per person ’)
legend ({’children ’,’pets ’},’Location ’,’bestoutside ’)

5.2.5 Line Chart

% Population change of bear from year to year in line chart


figure (8)
plot( Tanimal .Year , Tanimal .Bears);
grid on; xlabel (’Year ’); ylabel (’Population ’)
title (’Population change of bear from year to year in line chart ’)

26
% Population change of bears , dolphins , and whales
% from year to year in a single line chart
figure (9)
plot( Tanimal .Year , [ Tanimal .Bears Tanimal . Dolphins Tanimal . Whales ]);
grid on; xlabel (’Year ’); ylabel (’Population ’)
legend ({’Bears ’,’Dolphins ’,’Whales ’},’Location ’,’bestoutside ’)
title (’Population change of three selected animals each year in line chart ’)

27
5.2.6 Area Chart

The area graph has a very similar concept to the line graph. The data stacks with more than one
different data. In default, area graph interval always starts from zero, unlike the line graph, which
depends on the extreme values.

% Population change of bear from year to year in line chart


% dalam area chart
figure (10)
area( Tanimal .Year , Tanimal . Dolphins )
grid on; xlabel (’Year ’); ylabel (’Population ’)
title (’Population change of dolphins through the years ’)

28
% Population change of bears , dolphins , and whales
% from year to year in a single area chart
figure (11)
area( Tanimal .Year , [ Tanimal .Bears Tanimal . Dolphins Tanimal . Whales ]);
grid on; xlabel (’Year ’); ylabel (’Population ’)
legend ({’Bears ’,’Dolphins ’,’Whales ’},’Location ’,’bestoutside ’)
title (’Population change of three selected animals through the years ’)

29
5.2.7 Scatter and Bubble Plot

% Relationship between gold medals and total medals in scatter plot


illustrating
% positive correlation
figure (12)
scatter ( Tmedali .gold , Tmedali .total ,[],’magenta ’,’*’)
grid on; xlabel (’gold medal ’); ylabel (’total medal ’)
title (’Correlation between gold and total medals possession ’)

30
Where, the parameters scatter plot are (xaxis, yaxis, [], COLOR, ’*’).

Similar to scatter graph, the bubble graph has a third parameter represented by the size of the
bubble. More values, bigger bubble.

% Number of total medal for Top 10 countries correlated with gold medal
% possession at x-axis and silver medal at y-axis on bubble plot
figure (13)
Tmedali10 = Tmedali (1:10 ,:) % extract the first 10 data
scatter ( Tmedali10 .gold , Tmedali10 .silver , [ Tmedali10 .total],’filled ’, ’green ’)
grid on; xlabel (’gold medal ’); ylabel (’silver medal ’)
title (’Correlation between Total , Gold , and Silver medal possessions of the Top
10 ’)

Where, the bubble plot parameters are (xaxis, yaxis, [bubble size], COLOR, ’filled’)

31

Das könnte Ihnen auch gefallen