You are on page 1of 32

Modul Praktikum

Pengantar Teknologi Informasi


dan Algoritma
(dengan MATLAB)

Ade Irawan, Ph.D.

Semester 1 2018/2019
Experiment 1
Variable: Assignment, Class,
Value, Operator

1.1 Objectives
• To understand how to create and assign a variable.
• To understand various classes, the differences and the implementation.
• To understand data input and output.

1.2 Components and Equipments


• One desktop computer with MATLAB or Octave.

1.3 Theory in Brief


1.3.1 Integrated Development Environment (IDE)
MATLAB is a proprietary mathematical and graphical software package with
numerical, graphical, and programming capabilities. It has many built-in
tools for solving problems and developing graphical illustrations. Integrated
Development Environment (IDE) of MATLAB is illustrated in Figure 1.1.
It shows some important features for developing programs in MATLAB pro-
gramming language. There are a source code editor for creating scripts, a

1
EXPERIMENT 1. VARIABLE 2

Figure 1.1: IDE of MATLAB version 2016a.

workspace for showing variables and their values, as well as Current Folder
window for showing files in the working directory. The path of current work-
ing directory is given in the bar above the source code editor. In addition,
we can interactively use MATLAB by entering some expressions in the Com-
mand Window.
GNU Octave is a free software licensed under the GNU General Pub-
lic License (GPL) which has powerful mathematics-oriented syntax that are
largely compatible with MATLAB. The IDE of GNU Octave is illustrated in
Figure 1.2. Like in MATLAB IDE, there are source code editor, workspace,
current folder window, and command window in the IDE of GNU Octave.
Their functions are similar to those in MATLAB. Even though all commands
in this module are dedicated for MATLAB, they are supposed to be success-
fully executed by GNU Octave.

1.3.2 Variable
A variable is used to store a value. Typically, a variable has a name, a type or
class1 , and its value. The value assignment can be executed by using symbol
’=’ (without quotes). As for the variable name, there are some rules that
should be followed:
1
Type in MATLAB is called class.
EXPERIMENT 1. VARIABLE 3

Figure 1.2: IDE of Octave version 4.0.3.

• Must begin with a letter of the alphabet. Afterwards, it can contain


letters, digits, and the underscore character, but it CANNOT have a
space. Example: myVariable 1.

• Length of the name is NOT more than 63 characters.

• Case-sensitive, e.g. myVariable and MYVariable are different.

• Names of built-in functions can, but should not, be used as variable


name.

There are 7 kind of classes in MATLAB: logical, char, numeric, table, cell,
struct, and function handle (@). The size and range of numerical variable’s
classes are given in Table 1.1. A variable declaration is not necessary in
MATLAB as it will be assigned by MATLAB to the default class. By default,
the class of a numerical variable is double. The command class can be used
to check the class of a variable.
A class can be seen as a combination of a type and the operations that can
be performed on values of that type. Some operators, utilized in MATLAB,
are given in Table 1.2, with precedence ordered from highest to lowest level.
EXPERIMENT 1. VARIABLE 4

Class Size (Bytes) Min. Value Max. Value


single 4 −3.4 × 1038 3.4 × 1038
double 8 −1.79 × 10308 1.79 × 10308
int8 1 −128 127
int16 2 −32768 32768
int32 4 −2.14 × 109 2.14 × 109
int64 8 −9.22 × 1018 9.22 × 1018
uint8 1 0 255
uint16 2 0 65535
uint32 4 0 4.29 × 109
uint64 8 0 1.84 × 1019

Table 1.1: Numerical Variable’s Classes in MATLAB

Operation Operator
() parentheses
e exponential operation for the exponent
of 10 (e.g., 2 ∗ 10 ∧ 3 is equal to 2e3)
∧ exponentiation (e.g., 2 ∧ 3 = 8)
∗ multiplication
arithmetic
/ division (divided by, e.g. 6/3 = 2)
\ division (divided into, e.g. 6\3 = 0.5)
+ addition
− negation, substraction
< less than
<= less than or equal to
> greater than
relational
>= greater than or equal to
== equal to
∼= NOT equal to
∼ element-wise NOT
& element-wise AND
logical | element-wise OR
&& logical operator AND
|| logical operator OR

Table 1.2: Operators from highest to lowest precedence level in MATLAB.


EXPERIMENT 1. VARIABLE 5

1.3.3 Input and Output


MATLAB can get arbitrary input from the user with command input and
print out any result with command: fprintf. Those commands are useful
when exploiting MATLAB with script instead of Command Window. If you
unfamiliar with any of MATLAB commands that are used in the lab, please
use the help function2 .

1.4 Practice
1. Start MATLAB and type the following code in the script editor.
1 a = i n p u t ( ’ Enter your name : ’ , ’ s ’ ) ;
2 f p r i n t f ( ’ Good day , %s ! \n ’ , a )

Save the script with name: tryInputOutput.m. Run the script by


clicking the Run icon. Enter any name you like.
Question: What is the class of a ?

2. Change variable a in line 1 of Task 1 with variable name, then run the
script. Enter any different name with that in Task 1.
Question: What is the output shown in the Command Window ? Is it
the same with Task 1 or not? Why ?

3. Fix the source code so that the name that you have entered will be
shown when you run it. Note: keep using variable name instead of a.

4. Type the following in the Command Window


len check = length(name) <= 63;
Question: What is the class of len check ?

5. Write a simple script with an input: radius (r), and output: area, as
the area of a circle. Save the script with name: circleArea.m. Hint:
2
Type ’help anycommand ’ (without quote) in the Command Window.
EXPERIMENT 1. VARIABLE 6

use the constant pi.

Question: What is the class of the variable area?

Convert the class of area with the following command in Command


Window : int8(area).
Question: What happened with the value?

6. Evaluate the following value. Try to guess first.

(a) 3\6 ∧ 2
(b) ∼ (−2 > 3) & (3\12 >= 4)
(c) int8(128) + 1
(d) uint16(−20) ∗ 3
(e) int8(−129) < −128
Experiment 2
Selection Statements

2.1 Objectives
• To understand how to create and to use selection or branching state-
ments.

• To understand the boolean operation utilized in a selection statement.

2.2 Components and Equipments


• One desktop computer with MATLAB or Octave.

2.3 Theory in Brief


Statement is not always executed in sequence. In this experiment, we are
going to practice how to make choices as to whether statements are executed
or not, and how to choose between or among statements. The statements
that accomplish this are called selection or branching statements.

2.3.1 The If Statement


(One Case Selection Statement)
The if statement chooses whether another statement, or group of statements,
is executed or not. The general form of the if statement is

7
EXPERIMENT 2. SELECTION STATEMENTS 8

1 i f condition
2 action
3 end
4

A condition is a relational expression that is logically true or false. The


action is a statement, or a group of statements, that will be executed if the
condition is true.
Note that the concept of false in MATLAB is represented by the value 0,
but the concept of true can be represented by any nonzero value, i.e. not
just 1. To understand this concept, try the practice No. 2.
The if statement can be entered in the Command Window, although
they generally make more sense in scripts or functions. In the Command
Window, the if line would be entered, followed by the Enter key, the action,
again the Enter key, and finally end and Enter. The results will follow
immediately.

2.3.2 The If-Else Statement


The if-else statement is used to choose between two statements, or sets of
statements. The general form is
1 i f condition
2 action1
3 else
4 action2
5 end
6

First, the condition is evaluated. If it is true, then the set of statements


designated as action 1 is executed, and that is the end of the if-else
statement. But, if the condition is false, the second set of statements des-
ignated as action 2, and that is the end of the if-else statement. One
application of an if-else statement is error-checking, i.e., to check for errors
in the inputs. To understand the application, try the practice No. 3

2.3.3 The elseif Clause


To choose from among more than two actions, the elseif clause is used. For
example, if there are n choices, where n > 3 in this example, the following
general form would be used:
EXPERIMENT 2. SELECTION STATEMENTS 9

1 i f condition1
2 action1
3 e l s e i f condition2
4 action2
5 e l s e i f condition3
6 action3
7 else
8 actionn
9 end
10

Do NOT putting a space between else and if in the keyword elseif, i.e.,
else if.

2.3.4 The switch Statement


A switch statement can often be used in place of a nested if-else or an
if statement with many elseif clauses. switch statements are used when
an expression is tested to see whether it is equal to one of several possible
values. The general form of switch statement is
1 switch switch expression
2 case expression1
3 action1
4 case { expression2 .1 , expression2 .2 , expression 2.3}
5 action2
6 case expression3
7 action3
8 o t h e r w i s e %o p t i o n a l
9 actionn
10 end
11

The switch expression is compared, in sequence, to the case expressions


(expression1, expression2.1, expression2.2, etc). If the value of the
switch expression matches expression2.2, for example, then action2 is
executed and the switch statement ends. If the value of the switch expression
does not match any of the case expressions, the action after the word
otherwise is executed, i.e., actionn, if there is an otherwise (if not, no
action is executed). It is not necessary to have an otherwise clause.
EXPERIMENT 2. SELECTION STATEMENTS 10

2.3.5 Nested Statement


To choose from more than two actions, the if-else and switch statements
can be nested, meaning one statement inside of another. For example, the
following script is an implementation of the continuous mathematical func-
tion y = f (x):
y = 1 if x < −1
y = x2 if −1 ≤ x ≤ 2
y = 4 if x > 2
using three separate if statement.
1 i f x < −1
2 y = 1;
3 end
4
5 i f x >= −1 && x <= 2
6 y = xˆ2;
7 end
8
9 if x > 2
10 y = 4;
11 end
12

However, this is not a very efficient code: all three logical expressions must
be evaluated, regardless of the range in which x falls. If x is less than −1,
the first expression is true and 1 would be assigned to y. However, the next
two if statements’ expressions are still evaluated. Instead of writing it this
way, the statements can be nested so that the entire if-else statement ends
when an expression is found to be true:
1 i f x < −1
2 y = 1;
3 else
4 i f x <= 2
5 y = xˆ2;
6 else
7 y = 4;
8 end
9 end
10
EXPERIMENT 2. SELECTION STATEMENTS 11

2.3.6 The ”is” Function in MATLAB


There are a lot of functions that are built into MATLAB that test whether
or not something is true. These functions have names that begin with the
word ”is”. Some of them are summarized in the Table 2.1.

Function
isequal returns logical 1 if input arguments are equal,
or 0 if it is not.
isletter returns logical 1 if the character argument is a letter of the alphabet,
or 0 if it is not.
isempty returns logical 1 if a variable is empty,
or 0 if it is not.

Table 2.1: Some of ”is” functions in MATLAB


EXPERIMENT 2. SELECTION STATEMENTS 12

2.4 Practice
1. Write an If statement that would print ”Hey, you get overtime!” if
the value of a variable hours is greater than 2. Test the statement for
values of hours less than, equal to, and greater than 2. Try both in the
script editor and Command Window.
Would it be easier to do this in the Command Window or in a script ?
2. Write the following script in the editor, run with any input possible,
and discuss the results.
1 l e t t e r = i n p u t ( ’ Choice (Y/N) : ’ , ’ s ’ ) ;
2

3 i f l e t t e r == ’ y ’ | | l e t t e r == ’Y ’
4 d i s p ( ’ Action 1 ’ )
5 end
6
7 i f l e t t e r == ’ y ’ | | ’Y ’
8 d i s p ( ’ Action 2 ’ )
9 end
10

3. Write a script that will prompt the user for a monthly income. If the
user enters an invalid number, i.e, less than 0, display an error message,
for example: ”Sorry, it is not a valid number”. Otherwise, calculate the
tax money which is 10% of the income, and then print the result. Try
any of function: fprintf, error, or disp, to print out the message.
4. Write a script that will prompt the user for a quiz score in the range
from 0 to 10. Firstly, check the score whether or not it is in the range
and return an error message if it is not. Otherwise, returns a corre-
sponding letter grade according to the following scheme:
9 or 10 is an ’A’,
8 is a ’B’,
7 is a ’C’,
6 is a ’D’,
and anything below that is an ’F’.
Use elseif clause.
5. Use nested is-else and switch statements for the problem in No.4.
Hint: use if-else statement for error-checking and switch statement
for the rest.
EXPERIMENT 2. SELECTION STATEMENTS 13

6. In addition to No. 5, check the input whether or not it was empty.


Show warning message if the user does not input any number. Hint:
use function isempty.

7. Rewrite the following nested if-else statement as a switch statement


that accomplishes exactly the same thing.
1 num = i n p u t ( ’ Enter any number : ’ ) ;
2
3 i f num < −2 | | num > 4
4 d i s p ( ’ Action 1 ’ )
5 else
6 i f num <= 2
7 i f num >= 0
8 d i s p ( ’ Action 2 ’ )
9 else
10 d i s p ( ’ Action 3 ’ )
11 end
12 else
13 d i s p ( ’ Action 4 ’ )
14 end
15 end
16

8. Rewrite the following switch statement as one nested if-else state-


ment (elseif clauses may be used).
1 l e t t e r = i n p u t ( ’ Enter any c h a r a c t e r : ’ , ’ s ’ ) ;
2
3 switch l e t t e r
4 case ’x ’
5 disp ( ’ Hello ’ )
6 c a s e { ’ y ’ , ’Y ’ }
7 d i s p ( ’ Coming ’ )
8 c a s e ’Q ’
9 d i s p ( ’ Quit ’ )
10 otherwise
11 disp ( ’ Error ’ )
12 end
13
Experiment 3
Loop Statements

3.1 Objectives
• To understand how to create and to use looping statements.

3.2 Components and Equipments


• One desktop computer with MATLAB or Octave.

3.3 Theory in Brief


Looping statements allow other statement(s) to be repeated. There are two
basic kinds of loops: counted loops (using for statement), and conditional
loops (using while statement).

3.3.1 for statement


for statement is used when it is known how many times the statements will
be repeated. The general form of this statement is given by
1 f o r loopvar = range
2 action
3 end
4

14
EXPERIMENT 3. LOOP STATEMENTS 15

There are two elements in for statement: (1) loop variable that is used to
iterate through values, and (2) action of the loop which is the statement(s)
that is(are) repeated. The range can be specified using any vector, but
normally the easiest way is to use the colon operator like a:b:c, where a, b,
and c are the integer numbers indicating the start, the increment/decrement
interval, and the end of the range, respectively. The interval can be a positive
(increment) or negative number (decrement). When b is negative, a > c must
be satisfied.

Nested for loops


When the action of a loop is another loop, this is called a nested loop. The
general form of a nested for loop is as follows:
1 f o r loopvar one = rangeone % <−− o u t e r l o o p
2

3 % actionone i n c l u d e s the inner loop


4 f o r l o o p v a r t w o = rangetwo % <−− i n n e r l o o p
5 actiontwo
6 end
7
8 end
9

Combining Nested for Loops and If Statements


The statements inside of a nested loop can be any valid statements, including
any selection statement. Hence, there could be an if-else statement as the
action in a loop. Try practice No. 3.

3.3.2 while statement


The while statement is used to repeat an action when it is not known how
many times the action will be repeated. The general form of this statement
is given by
1 while condition
2 action
3 end
4
EXPERIMENT 3. LOOP STATEMENTS 16

The actions, which consists of any number of statement(s), is executed as


long as the condition is true. Firstly, the condition is evaluated. If the
condition is true, and the action is executed, the condition is evaluated again.
Eventually, something in the action has to change something in the condition
so it becomes false to avoid an infinite loop. If this happens, Ctrl-C will exit
the loop.
EXPERIMENT 3. LOOP STATEMENTS 17

3.4 Practice
1. (a) Write a for loop script that will print a column of five * as the
following
1 ∗
2 ∗
3 ∗
4 ∗
5 ∗
6

(b) Write a for loop script that will print a row of five * as the
following
1 ∗ ∗ ∗ ∗ ∗
2

2. Using nested for loop, write a script that will print a triangle of stars
as the following
1 ∗
2 ∗ ∗
3 ∗ ∗ ∗
4

Hint: use the loop variable of the outer loop in the range of the inner
loop.
3. Trace the following script to figure out what the result will be, and
then type it into MATLAB or Octave to verify the result.
1 vec = [ 5 0 3 −2 10 13 −5 1 7 −1];
2 outmin = vec ( 1 ) ;
3
4 f o r i = 2 : l e n g t h ( vec )
5 i f vec ( i ) < outmin
6 outmin = vec ( i ) ;
7 end
8 end
9
10 d i s p ( [ outmin ] )
11

4. (a) Trace the following script to figure out what the result will be,
and then type it into MATLAB or Octave to verify the results.
EXPERIMENT 3. LOOP STATEMENTS 18

1 count = 0 ;
2 number = 1 0 ;
3
4 w h i l e number > 0
5 f p r i n t f ( ’ number i s %d \n ’ , number )
6 number = number − 2 ;
7 count = count + 1 ;
8 end
9 f p r i n t f ( ’ count i s %d \n ’ , count )
10

(b) Write the equivalent result with for loop.

5. Trace the following script to figure out what the result will be, and
then type it into MATLAB or Octave to verify the result. Try to enter
negative number(s).
1 n = 10;
2 f p r i n t f ( ’ P l e a s e e n t e r %d p o s i t i v e numbers \n\n ’ , n ) ;
3 for i = 1:n
4 inputnum = i n p u t ( ’ Enter a p o s i t i v e number : ’ ) ;
5 w h i l e inputnum < 0
6 inputnum = i n p u t ( ’ I n v a l i d ! Enter a p o s i t i v e number : ’ ) ;
7 end
8 f p r i n t f ( ’ Thanks , you e n t e r e d a %d \n ’ , inputnum ) ;
9 end
Experiment 4
Function

4.1 Objectives
• To understand how to create and to use function.

4.2 Components and Equipments


• One desktop computer with MATLAB or Octave.

4.3 Theory in Brief


A modular program is a program in which the solution is broken down into
modules, and each is implemented as a function. A function can be called
via Command Window or via a script. The script that calls these functions
is typically called the main program. The general form of a function is given
as follows
1 f u n c t i o n [ outputarguments ] = functionname ( i n p u t arguments )
2 % comment d e s c r i b i n g t h e f u n c t i o n
3
4 % S t a t e m e n t s / a l g o r i t h m s a r e here , which o p t i o n a l l y i n c l u d e t h e
i n p u t arguments and p u t t i n g v a l u e s i n t h e output arguments
5
6 end

The name of the file must be the same as the function’s name.

19
EXPERIMENT 4. FUNCTION 20

It is also possible to have another function inside a function. The function


that call another function is named primary function, while the function
being called is termed sub-function.
EXPERIMENT 4. FUNCTION 21

4.4 Practice
1. Given the following function header :
1 function doit (a , b)

Which of the following function calls would be valid ? why ?


1 f p r i n t f ( ’ The r e s u l t i s %d \n ’ , d o i t ( 5 , 1 2 ) )

1 do i t (3 , 6 , 9)

1 x = 3.4;
2 y = 7.2;
3 doit (x , y)

2. Write a function calcTriangle that will calculate and return the area
of a triangle. Pass the base and the height to the function as input
arguments.

3. A function can return a vector as a result. Write a function vecout


that will receive one integer argument and will return a vector that
increments form the value of the input argument to its value plus 10,
using the colon operator. For example,
1 vecout (3)
2 ans =
3 3 4 5 6 7 8 9 10 11 12 13

4. Write four separate functions to calculate the volume and surface area
of a hollow cylinder by accomplishing these tasks:

(a) A function to prompt the user and read in the radius of the cylin-
der base and the height of the cylinder.
(b) A function to calculate and return the volume of the cylinder.
(c) A function to calculate and return the surface area of the. cylinder
(d) A function to display the results.

Note: The volume is given by πr2 h, and the surface area is 2πrh
Experiment 5
Array

5.1 Objectives
• To understand how to create, to modify, and to use scalar, vectors, and
matrices.

5.2 Components and Equipments


• One desktop computer with MATLAB or Octave.

5.3 Theory in Brief


An array is a collection of data objects of the same type. The data objects
can themselves be arrays. A vector in MATLAB is equivalent to what is
called a one-dimensional array in other languages, whereas matrix is a two-
dimensional array. Vector has dimension r×1, i.e. column vector, or 1×c, i.e.
row vector, where r and c are the number of row and column, respectively.
Likewise, a scalar is the special case of matrix having dimension 1 × 1. All
of the values stored in these arrays are stored in what are called elements.

5.3.1 Creating Vectors and Matrices


There are several ways to create row vector variables. For example:

22
EXPERIMENT 5. ARRAY 23

1 rv1 = [ 1 3 5 7 ]
2 rv2 = [ 1 , 3 , 5 , 7 ]
3 rv3 = 1 : 2 : 7 % colon operator c r e a t e s vector as
4 % min . v a l u e : s t e p v a l u e : max . v a l u e
5
6 rv4 = l i n s p a c e ( 1 , 2 , 5 ) % l i n s p a c e f u n c t i o n c r e a t e s
7 % e q u a l l y spaced vector .
8 rv5 = z e r o s ( 1 , 4 )
9 rv6 = o n e s ( 1 , 4 )

Likewise, we can create column vector variables as the following


1 cv1 = rv1 ’ % assume rv1 i s a row v e c t o r v a r i a b l e
2 % ’ i s t h e matrix t r a n s p o s e o p e r a t o r
3 cv2 = [1 3 5 7] ’
4 cv3 = [1 , 3 , 5 , 7] ’
5 cv4 = (1:2:7) ’
6 cv5 = [ 1 ; 3; 5; 7]
7 cv6 = zeros (4 ,1)
8 cv7 = ones ( 4 , 1 )

A variable can be assigned as a matrix with certain size and element


values, for instance
1 m1 = [1 2 3 4; 5 6 7 8]
2 m2 = [ [ 1 ; 2 ; 3 ; 4 ] [ 5 ; 6 ; 7 ; 8 ] ] % e q u a l t o m1’
3 m3 = zeros (2 ,4)
4 m4 = ones ( 2 , 4 )

Moreover, a matrix can be formed from several vectors like the following
1 rv1 = [ 1 , 3 , 5 , 7 ] ;
2 rv2 = 1 : 2 : 7 ;
3 m5 = [ rv1 ; rv2 ]
4
5 cv1 = [ 1 , 3 , 5 , 7 ] ’ ;
6 cv2 = [ 1 ; 3 ; 5 ; 7 ] ;
7 m6 = [ cv1 cv2 ]

5.3.2 Access The Element(s) of a matrix


The indices in MATLAB start at 1. The indices of a matrix m is illustrated
in Figure 5.1 If a single index is used with a matrix, MATLAB release the
matrix column by column. This is called linear indexing. MATLAB stores
matrices in memory in columnwise, which is why linear indexing refers to
EXPERIMENT 5. ARRAY 24

Figure 5.1: Access The Elements of Matrix m

the elements in order by columns. For example, with the matrix m shown in
Figure 5.1,
1 >> m( [ 1 2 4 ] )
2 ans =
3 3 4 6

5.3.3 Modifying Elements


The value stored in a matrix can be changed by specifying the index and
then assigning a value to it. For example,
1 >> m( 2 , 3 ) = 9
2 m=
3 3 5 7
4 4 6 9

An entire row or column could also be changed, by using colon operator. For
example,
1 >> m( : , 2 : 3 ) = 2
EXPERIMENT 5. ARRAY 25

2 m=
3 3 2 2
4 4 2 2

An existing matrix could also be extended by adding other vector or matrix


having the same number of row or column. For example, the following would
add a third row to the matrix m shown in Figure 5.1.
1 >> rv = [ 1 2 3 ] ;
2 >> m = [m; rv ]
3 m=
4 3 5 7
5 4 6 8
6 1 2 3

If there is a gap between the current matrix and the row or column being
added, MATLAB will fill in with zeros. For example,
1 >> m( : , 5 ) = [ 2 8 ]
2 m=
3 3 5 7 0 2
4 4 6 8 0 8

5.3.4 Matrix Operations

Operation Algebraic Form MATLAB Form Comments


Addition A+B A+B element-by-element
Subtraction A−B A−B element-by-element
A×B A*B matrix multiplication
Multiplication
A .* B element-by-element
A ./ B element-by-element,
a(i,j)/b(a,j)
Division A .\ B b(i,j)/a(a,j)
−1
A×B A/B equal to A*inv(B)
A−1 × B A\B equal to inv(A)*B
Inverse A−1 inv(A)
Transpose AT A’
Determinant |A| det(A)

Table 5.1: Matrix Operations in MATLAB


EXPERIMENT 5. ARRAY 26

Logical operations to a matrix will return 0s or 1s according to the oper-


ator. For example,
1 >> m = [ 3 5 7
2 4 6 8];
3 >> m > 4
4 ans =
5

6 2 x 3 l o g i c a l array
7
8 0 1 1
9 0 1 1
EXPERIMENT 5. ARRAY 27

5.4 Practice
1. How can you use the colon operator, i.e. ”:”, to generate the vector
shown below?
1 v =
2 10 7 4 1 −2 −5

2. (a) Create a random vector variable v with dimension 5 × 1, and


subtract 5 from every element in it.
(b) Create a random matrix variable m1 with dimension 3 × 4, and
divide every element by 2.
(c) Create a random matrix variable m2 with dimension 3 × 4, and
multiply every element by 3.
(d) Create a random matrix variable m3 with dimension 5 × 3, and
square every element.
(e) Using existing v, m1, m2, and m3 created above, write any matrix
operation possible, by combining any of those vector and matrices.
(at least 4)

3. Let array
1 >> A =
2 [1 1 0 1
3 0 2 0 2
4 3 1 1 3]

Write commands that will perform each of the following operations on


array A:

• Return the second column of A.


• Return the first and third rows of A.
• Return the third value of the second row and the third value of
the fourth column of A, in a row vector.
• Append the column vector [7; 8; 9] to A.
• Append the row vector [2 6 3 4] to A.

Re-create array A again before each problem. Check your answer by


using Octave or MATLAB.
EXPERIMENT 5. ARRAY 28

4. Generate a vector of 20 random integers, each in the range from 50 to


100. Create a variable events that stores all of the even numbers from
the vector, and a variable odds that stores the odd numbers. (hint: use
rem function or mod function with the vector as one of the function’s
input argument)
Reference

Stormy, A., (2017). MATLAB: A Practical Introduction to Programming and


Problem Solving, 4th Edition, Butterworth-Heinemann, Cambridge, USA

29

You might also like