Professional Documents
Culture Documents
Chapter 3
Chapter 3
Chapter 3
Introduction to . . .
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
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
Logical operators
Bitwise operations Fundamentals of . . .
Set operations Introduction to . . .
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
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
specifying variables.
Close
• Similarly: JJ II
>>5 ∧ 3 J I
ans=
Page 7 of 37
125
>>2.5 ∧ 2 Go Back
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
>>25*3;
Close
Title Page
Operator Precedence
JJ II
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
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
The symbols &, |, and v are the logical array oper- Introduction to . . .
on logical operations.
Title Page
Colon, : Close
Subscripting, () Quit
Brackets, []
Decimal point, . Fundamentals of . . .
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
Close
Assignment of Variables in MATLAB
Quit
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
√ 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
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
see.
∅ There are two basic kinds of MATLAB vectors: Row Fundamentals of . . .
and Column vectors. Introduction to . . .
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 . . .
>>u=[1 2 3]
u= Fundamentals of . . .
123 Introduction to . . .
>>v2=[3+4 5] J I
v2=
Page 20 of 37
75
>>v3=[3 +4 5] Go Back
345
Close
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
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
w=
4567 Fundamentals of . . .
>>w(4)=[];w Introduction to . . .
w=
456
∅ The elements of a vector can be also defined with Home Page
∅ 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
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
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
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
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
0
Close
0.6931
1.0986 Quit
>>pi*v
ans= Fundamentals of . . .
3.1416 Introduction to . . .
6.2832
9.4248
Home Page
JJ II
∅ Multiplication of vectors and matrices must follow
special rules. J I
∅ 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
246 J I
4 8 12
Page 30 of 37
6 12 18
>>v’*b Go Back
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
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
Page 33 of 37
The colon operator
Go Back
12345 JJ II
Full Screen
∅ Thus,
>>b=0:0.2:1 Close
b= Quit
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
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