Chapter 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Fundamentals of . . .

Introduction to . . .

Introduction to Computer Basics Home Page

and Programming Title Page

JJ II

J I
Basic Operations, Vectors and Arrays
Page 1 of 37
School of Chemical and Bio Engineering
Addis Ababa Institute of Technology Go Back

Addis Ababa University Full Screen


teklebrahan.gebrekrstos@aait.edu.et
Close

Quit
October 26, 2015
Basic Operations, Vectors and Arrays Fundamentals of . . .

Introduction to . . .
• An operator is a symbol that tells the compiler
to perform specific mathematical or logical ma-
nipulations. Home Page

• MATLAB is designed to operate primarily on whole Title Page

matrices and arrays. JJ II

• Therefore, operators in MATLAB work both on J I

scalar and non-scalar data. Page 2 of 37

• MATLAB allows the following types of elemen- Go Back

tary operations; Full Screen

Arithmetic operators Close

Relational operators Quit

Logical operators
Bitwise operations Fundamentals of . . .
Set operations Introduction to . . .

• For the purpose of this course we will contained


ourselves on arithmetic operators relational op-
Home Page
erators and logical operators.
Title Page

Arithmetic Operators JJ II

J I
• There are three kinds of numbers used in MAT-
Page 3 of 37
LAB:
Go Back
1. integers
Full Screen
2. real numbers
3. complex numbers Close

• In addition to these, MATLAB has three variables Quit

representing non-numbers: (-Inf, Inf and NaN).


• The -Inf and Inf are the negative and positive in- Fundamentals of . . .
finity respectively. Introduction to . . .

• Infinity is generated by overflow or by operation


of dividing by zero.
Home Page

• The NaN stands for Not-a-Number and it is ob-


Title Page
tained as a result of the mathematically unde-
fined operations such as 0/0, ∞/∞ etc. JJ II

J I
• MATLAB allows two different types of arithmetic
operations. Page 4 of 37

Go Back
Matrix arithmetic operations
Array arithmetic operations Full Screen

Close
• Matrix arithmetic operations are same as defined
in linear algebra. Quit
• Array operations are execute element by element, Fundamentals of . . .
both on one-dimensional and multidimensional Introduction to . . .
array.
• The matrix operators and array operators are dif-
Home Page
ferentiated by the period (.) symbol.
Title Page
• However, as the addition and subtraction oper-
ation is same for matrices and arrays, the opera- JJ II

tor is same for both cases. J I

• The list of basic arithmetic operations in MAT- Page 5 of 37

LAB includes six operations. Go Back

• In our present context we shall consider these Full Screen

operations as scalar arithmetic operations which Close


is to say that they operate on two numbers in the
Quit
conventional manner.
Symbol Operation
Fundamentals of . . .
+ Addition
Introduction to . . .
- Subtraction
* Multiplication
/ Right division
 Left division Home Page
∧ Power
Title Page

• MATLAB’s arithmetic operations are actually much JJ II


more powerful than this. J I

• We shall see just a little of this extra power later Page 6 of 37

in vector and matrix operations. Go Back

• One can use MATLAB like a calculator without Full Screen

specifying variables.
Close

• MATLAB has all the standard mathematical op- Quit


erations.
Example: Try type Fundamentals of . . .
>>2+3 Introduction to . . .

• MATLAB returns the answer:


Home Page
ans=
5 Title Page

• Similarly: JJ II

>>5 ∧ 3 J I
ans=
Page 7 of 37
125
>>2.5 ∧ 2 Go Back

ans= Full Screen

6.2500
Close
>>3.98*4.1
ans= Quit

15.9490
>>99.3-25 Fundamentals of . . .
ans= Introduction to . . .
74.3000
>>3*(23+14.7-4/6)/3.5
ans= Home Page

31.7429
Title Page

JJ II
• The result of these operations is assigned to de-
J I
fault variable called ans and displayed.
Page 8 of 37
• Adding a semicolon(;) to the end of the opera-
tion suppresses the output. Go Back

Example: Try type Full Screen

>>25*3;
Close

• Also type: 1/0 Quit

Warning: Divide by zero.


ans= Fundamentals of . . .
Inf Introduction to . . .
>> Inf/Inf
ans=
NaN Home Page

Title Page
Operator Precedence
JJ II

• The order in which expressions are evaluated in J I


MATLAB is fixed according to:
Page 9 of 37

1. (expressions in parentheses) take precedence over Go Back

2. powers, ∧ which take precedence over Full Screen

3. multiplication and division, *,/ which, in turn, Close

take precedence over Quit

4. addition and subtraction, +,-


Relational Operators Fundamentals of . . .

Introduction to . . .
 Relational operators for arrays perform element-
by-element comparisons between two arrays and
return a logical array of the same size, with ele- Home Page
ments set to logical 1 true where the relation is
true and elements set to logical 0 false where it Title Page

is not. JJ II

 Relational operators can also work on both scalar J I

and non-scalar data. Page 10 of 37

 The following table shows the relational operators Go Back

available in MATLAB: Full Screen

Close

Quit
Operator Description
Fundamentals of . . .
< Less than
Introduction to . . .
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to Home Page
v= Not equal to
Title Page

Logical Operators JJ II

J I
 MATLAB offers two types of logical operators and
functions: Page 11 of 37

Go Back
1. Element-wise: These operators operate on
corresponding elements of logical arrays. Full Screen

2. Short-circuit: These operators operate on Close

scalar and, logical expressions. Quit

 Element-wise logical operators operate element-


by-element on logical arrays. Fundamentals of . . .

 The symbols &, |, and v are the logical array oper- Introduction to . . .

ators AND, OR, and NOT respectively.


 Short-circuit logical operators allow short-circuiting Home Page

on logical operations.
Title Page

 The symbols && and || are the logical short-circuit JJ II


operators AND and OR respectively.
J I
 Further, there is a menu of special characteristics Page 12 of 37
that have specific uses.
Go Back

 The main ones are:


Full Screen

Colon, : Close

Subscripting, () Quit

Brackets, []
Decimal point, . Fundamentals of . . .

Continuation, ... Introduction to . . .

Separator, ,
Semicolon, ; (suppresses the output of a cal-
Home Page
culation)
Title Page
Assignment, =
Quote, ’statement’ JJ II

Transpose, ’ J I

Comment, % Page 13 of 37

Note: Anything after % is a comment and will be Go Back

ignored by MATLAB. Full Screen

Close
Assignment of Variables in MATLAB
Quit

 Variable names may be up to 19 characters long.


 Names must begin with a letter but may be fol- Fundamentals of . . .
lowed by any combination of letters, numbers/digits Introduction to . . .
or underscores.
 MATLAB is case sensitive in the naming of vari-
Home Page
ables.
Title Page

Predefined variables JJ II

J I
 There are several predefined variables which can
be used at any time, in the manner as user de- Page 14 of 37

fined variables. Go Back

 These are, (ans, pi, eps, j and i). Full Screen

√ Close
 Where i & j are −1, pi is 3.1416...and eps is the
small number in MATLAB which has a value 2.2204× Quit

10−16 .
Example: Try type: Fundamentals of . . .
>>pi Introduction to . . .
ans=
3.1416
>>eps Home Page

ans=
Title Page
2.2204 × 10−16
>>j JJ II

ans= J I
0 + 1.0000i
Page 15 of 37
>>y=2*(1+4*j)
y= Go Back

2.0000 + 8.0000i Full Screen

Close

Quit
Vectors and Arrays Fundamentals of . . .

Introduction to . . .
∅ An array is a collection of numbers, called elements,
referenced by one or more indices running over
different index sets. Home Page

∅ In MATLAB, the index sets are always sequential Title Page

integers starting with one.


JJ II

∅ The dimension of the array is the number of in- J I


dices needed to specify an element.
Page 16 of 37

∅ The size of an array is a list of the sizes of the index Go Back


sets.
Full Screen

∅ A matrix is a two-dimensional array with special


Close
rules for addition, multiplication, and other op-
erations. Quit
∅ The two dimensions are called the rows and the Fundamentals of . . .
columns. Introduction to . . .

∅ A vector is a matrix in which one dimension has


only the index 1.
Home Page

∅ A row vector has only one row and a column vector


Title Page
has only one column.
JJ II
∅ In MATLAB the word vector should really be inter-
J I
preted simply as ”list of numbers”.
Page 17 of 37
∅ Strictly it could be a list of other objects than num-
Go Back
bers but ”list of numbers” will fit our needs for
now. Full Screen

∅ These can be used to represent physical vectors Close

but are much more versatile than that as we shall Quit

see.
∅ There are two basic kinds of MATLAB vectors: Row Fundamentals of . . .
and Column vectors. Introduction to . . .

∅ As the names suggest, a row vector stores its num-


bers in a long ”horizontal list” such as;
Home Page
1, 2, 3. 4, 1. 23, -10. 3, 2. 1
which is a row vector with 6 components. Title Page

∅ A column vector stores its numbers in a vertical JJ II

list such as; J I


1
Page 18 of 37
2
3. 4 Go Back

1. 23 Full Screen

-10. 3
Close
2. 1
which is a column vector with (the same) 6 com- Quit

ponents.
∅ In mathematical notation these arrays are usually Fundamentals of . . .
enclosed in brackets []. Introduction to . . .

Entering vectors in MATLAB


Home Page

∅ To enter a vector explicitly, there are a few basic Title Page


rules to follow:
JJ II
• Separate the elements of a row with spaces
J I
or commas.
Page 19 of 37
• Use a semicolon; or returns, to indicate the
end of each row. Go Back

• Surround the entire list of elements with square Full Screen

brackets, []. Close

Example: Enter the following 3-by-1 row vector Quit

>>u=[1 2 3]
u= Fundamentals of . . .
123 Introduction to . . .

∅ The entries must be enclosed in square brackets.


>>v=[1 3 sqrt(5)]
Home Page
v=
1.0000 3.0000 2.2361 Title Page

∅ Space is very important. JJ II

>>v2=[3+4 5] J I
v2=
Page 20 of 37
75
>>v3=[3 +4 5] Go Back

v3= Full Screen

345
Close

∅ We can do certain arithmetic operations with vec- Quit

tors of the same length, such as v and v3 in the


previous section. Fundamentals of . . .
>>v+v3 Introduction to . . .
ans=
4.0000 7.0000 7.2361
>>v4=3*v Home Page

v4=
Title Page
3.0000 9.0000 6.7082
>>v5=2*v-3*v3 JJ II

v5= J I
-7.0000 -6.0000 -10.5279
Page 21 of 37

∅ We can build row vectors from existing ones: Go Back


>>w=[1 2 3], z=[8 9]
Full Screen
w=
123 Close

z= Quit
89
>>v6=[w z] Fundamentals of . . .
v6= Introduction to . . .
12389
∅ A vector can be defined by previously defined vec-
Home Page
tors.
>>x=[2 4 -1] Title Page

x= JJ II
2 4 -1
J I
>>x1=[x 5 8]
x1= Page 22 of 37

2 4 -1 5 8 Go Back

∅ An special array is the empty matrix, which is en- Full Screen

tered as [] and can be used to delete a part of any


Close
vector.
>>w=[4 5 6 7] Quit

w=
4567 Fundamentals of . . .
>>w(4)=[];w Introduction to . . .
w=
456
∅ The elements of a vector can be also defined with Home Page

algebraic expressions placed at the appropriate Title Page

location of the elements. JJ II

∅ Thus, J I
>>a=[sin(pi/2) sqrt(2) 3+4 6/3 exp(2)]
Page 23 of 37
a=
1.0000 1.4142 7.0000 2.0000 7.3891 Go Back

Full Screen

Column Vectors Close

∅ The column vectors have similar constructs to row Quit

vectors.
∅ When defining them entries are separated by ;or Fundamentals of . . .
”newlines”. Introduction to . . .
Example: To define a column vector x:
>>x=[1;-2;4]
x= Home Page

1
Title Page
-2
4 JJ II

J I
∅ or write
>>x=[1 Page 24 of 37

-2 Go Back
4]
Full Screen
x=
1 Close

-2 Quit
4
>>c=[1;3;sqrt(5)] Fundamentals of . . .
c= Introduction to . . .
1.0000
3.0000
2.2361 Home Page

∅ We can convert a row vector into a column vector Title Page

and vice versa by a process called transposing, JJ II


denoted by ’ in MATLAB.
J I
>>A=[1 2 3]
A= Page 25 of 37

123 Go Back
>>B=A’
Full Screen
B=
1 Close

2 Quit
3
Vectors addition and subtraction Fundamentals of . . .

Introduction to . . .
∅ Addition and subtraction of a number to or from a
vector can be made.
∅ In this case, the number is added or subtracted Home Page

from all elements of the vector. Title Page

Example:
JJ II
>>x=[-1;0;2];
y=x-1 J I

y= Page 26 of 37

-2
Go Back
-1
1 Full Screen

Close
∅ If we look to make a simple addition and subtrac-
tion of vectors. The notation is the same as found Quit

in most linear algebra.


∅ We will define two vectors then add or subtract Fundamentals of . . .
them: Introduction to . . .
Example:
>>v=[1;2;3]
v= Home Page

1
Title Page
2
3 JJ II

>>b=[2;4;6] J I
b=
Page 27 of 37
2
4 Go Back

6 Full Screen

>>v+b
Close
ans=
3 Quit

6
9 Fundamentals of . . .
>>v-b Introduction to . . .
ans=
-1
-2 Home Page

-3
Title Page
>>sin(v)
ans= JJ II

0.8415 J I
0.093
Page 28 of 37
0.1411
>>log(v) Go Back

ans= Full Screen

0
Close
0.6931
1.0986 Quit

>>pi*v
ans= Fundamentals of . . .
3.1416 Introduction to . . .
6.2832
9.4248
Home Page

Vectors multiplication Title Page

JJ II
∅ Multiplication of vectors and matrices must follow
special rules. J I

∅ In the example above, the vectors are both column Page 29 of 37

vectors with three entries. Go Back

∅ You can not add a row vector to a column vector. Full Screen

Close
∅ In the multiplication of vectors, the number of columns
of the vector on the left must be the number of Quit

rows of the vector on the right.


Example: Fundamentals of . . .
>>b=[2;4;6]; Introduction to . . .
>>v=[1;2;3];
>>v*b
??? Error using ==> ∗ Home Page

Inner matrix dimensions must agree.


Title Page
>>v*b’
ans= JJ II

246 J I
4 8 12
Page 30 of 37
6 12 18
>>v’*b Go Back

ans= Full Screen

28
Close

Quit
Element-wise operations Fundamentals of . . .

Introduction to . . .
∅ There are many times where we want to do an op-
eration to every entry in a vector or matrix.
∅ Matlab will allow you to do this with element-wise Home Page

operations. Title Page

∅ For example, suppose you want to multiply each JJ II

entry in vector v with its corresponding entry in J I


vector b.
Page 31 of 37

∅ In other words, suppose you you want to find v(1)*b(1), Go Back


v(2)*b(2) and v(3)*b(3).
Full Screen

∅ It would be nice to use the ”*”symbol since you


Close
are doing some sort of multiplication, but since
it already has a definition, we have to come up Quit

with something else.


∅ The programmers who came up with MATLAB de- Fundamentals of . . .
cided to use the symbols ”.*” to do this. Introduction to . . .
>>v.*b
ans=
2 Home Page

8
Title Page
18
JJ II
∅ Also for division and exponentiation we must use
J I
”./” and ” .∧ ” respectively.
>>v./b Page 32 of 37

ans= Go Back
0.5000
Full Screen
0.5000
0.5000 Close

∅ Note that; Quit


Θ v.*b multiplies each element of v by the re- Fundamentals of . . .
spective element of b. Introduction to . . .
Θ v./b divides each element of v by the respec-
tive element of b.
Θ v.\b divides each element of b by the respec- Home Page

tive element of v. Title Page

Θ v. ∧ b raises each element of v by the respec- JJ II


tive element of b.
J I

Page 33 of 37
The colon operator
Go Back

∅ The colon operator ”:” is understood by MATLAB


Full Screen
to perform special and useful operations.
Close
∅ If two integer numbers are separated by a colon,
MATLAB will generate all of the integers between Quit

these two integers.


∅ In particular, you will be able to use it to extract or Fundamentals of . . .
manipulate elements of matrices. Introduction to . . .

∅ The following command crates a row vector whose


components increase arithmetically:
Home Page
>>1:5
ans= Title Page

12345 JJ II

∅ And if three numbers, integer or non-integer, are J I


separated by two colons, the middle number is
Page 34 of 37
interpreted to be a step size and the first and
third are interpreted to be limits. Go Back

Full Screen
∅ Thus,
>>b=0:0.2:1 Close

b= Quit

0 0.2000 0.4000 0.6000 0.8000 1.0000


∅ Suppose you want to create a vector with elements Fundamentals of . . .
between 0 and 20 evenly spaced in increments Introduction to . . .
of 2.
∅ Then you have to type:
Home Page
>>t=0:2:20
t= Title Page

0 2 4 6 8 10 12 14 16 18 20 JJ II
>>m=0.32:0.1:0.6
J I
m=
0.3200 0.4200 0.5200 Page 35 of 37

>>w=-1.4:-0.3:-2 Go Back
w=
Full Screen
-1.4000 -1.7000 -2.0000
Close
∅ The format is first:step:last.
Quit

∅ The result is always a row vector.


∅ A negative step is also allowed. The command has Fundamentals of . . .
similar result; it creates a vector with linearly spaced Introduction to . . .
entries.

Referencing elements Home Page

Title Page
♦ It is frequently necessary to call one or more of the
elements of a vector. JJ II

J I
♦ Each dimension is given a single index.
Example: Page 36 of 37

>>A=0:10:100 Go Back
A=
Full Screen
0 10 20 30 40 50 60 70 80 100
Close
♦ Now after definition of vector A try to type:
>>A(10) Quit

ans=
Fundamentals of . . .

Introduction to . . .
90
>>B=A(1:5) Home Page

B=
0 10 20 30 40 Title Page

>>C=A(1:2:10)
C= JJ II

0 20 40 60 80
>>A(6:2:10) J I

ans=
50 70 90 Page 37 of 37

Go Back

Full Screen

Close

Quit

You might also like