Introduction To Matlab Tutorial 2 3
Introduction To Matlab Tutorial 2 3
Tutorial 2 and 3:
Goals
Variables
Variables
Read it
Use it for computation / change its value
and save it back to memory
Variable Assignment
max_grade = 100;
Variable identifier
(name):
Letters
Digits
Underscore
Value
Assignment
operator
clear max_grade;
clear;
clear all;
5
Variable - Hands On
Exercise 1:
Consider the
x=50;
Dont override built-in
functions!!!
disp=100;
disp(x);
builtin(disp,x);
cleardisp;
disp(x);
What happened?
Exercise 2:
Workspace functions
which(linspace);
clear x;
save(file_name, var1, var2, )
load(file_name);
Example:
Data Types
Next lectures
10
Default
11
Integer:
a = int16(100);
Be careful of
memory overflow
b = int8(5000);
Real(/Complex):
x = double(235.5);
x = single(235.5);
x = 235;
12
Notice:
Infinite
Is Not A
Number
13
Arrays Manipulation
14
Creating arrays
a = [1 2 3 4];
b = [1, 2, 3, 4];
c = [1; 2; 3; 4];
d =1:1:4;
in same line
linspace(0,100,51);
Min value
Max
value
Number of
points
logspace(1,10,11);
Same, but logarithmically spaced
between 10^1 and 10^10
16
Reminder:linspac
Use the commands we learned to define
e x as:
[ ,0.9 ,0.8 ,0.7 ,0.6 ,0.5 ,0.4 ,0.3 ,0.2 ,0.1 ,0,0.1 ,0.2 ,0.3 ,0.4 ,0.5 ,0.6 ,0.7 ,0.8 ,0.9 , ]
x = linspace(-pi,pi, 21);
% another way: x = (-1:0.1:1) * pi;
y = sin(x);
z = cos(x);
17
Multidimensional Array
Indexing
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
B(:, :, 1) = A;
B(:, :, 2) = A;
B(:, :, 3) = A;
Second
dimension
A=
(Column)
First
dimension
(Row)
size(A) ans = 3
A(2,3) <->A(8)
2
5
8
3
6
9
Third dimension
(Page)
1
4
7
1
4
7
2
5
8
3
6
9
B=
1
2
4 15 2
7 4 81 5
7 48
7
3
6 3
92 6 3
59 6
8
9
18
Arrays Manipulation
a = [1, 2; 3, 4]
2
4
ans=
1
3
2
4
a % or a.
a(2)
a(:)
a=
1
3
Array flattening
The last
coordinate in the
dimension
a(1,end:-1:1)
ans=
1
2
3
4
ans =
3
ans =
2
1
b = fliplr(a)
ans =
2
1
4
3
c = flipud(a)
ans =
3
4
1
2
19
e=
2
10
1
2
3
4
5
6
7
8
9
10
g = [d(1:3), e]
g=
1
reshape(a,1,4)
ans =
1
3
repmat(e,2,2)
ans =
2 4
2 4
d = 1:2:9
e = 2:2:10
f = [d; e]
Vertical Array
Concatenation
Horizontal Array
Concatenation
f=
6
6
8 10 2
8 10 2
4
4
10
6
6
8
8
10
10
1 2 3
4 5 6
7 8 9 ?
1
4
7
0
2
5
8
0
3 0
6 0
9 0
0 10
21
1
Notice the 4
difference 7
!
21
24
Subscripted
assignment dimension
Scalar expansion
2
5
8
3
6
9
22 23
25 26
22
Standard Arrays
What is it good for?
zeros(10,10)
ones(10,10)
rand(10,10)
randn(10,10)
eye(10,10)
Allocate memory
in advance!
Create a sample.
Identity matrix
1
0
0
0
1
0
0
0
1
24
25
Short Example
A=magic(6)
A(:,:)=ones(7,7)
???Subscriptedassignmentdimensionmismatch.
A(find(A>20))=100;
A=A(:,4:6);%orA(:,1:3)=[];
mean(A(4,:))
26
Arrays mathematical
operations
27
Array arithmetic
Element by element:
A+B
A-B
-A
A.*B
A./B
A.\B
.^
C = A*B :
Matrices dimensions
must match
+ * /\ by scalar are
element-wise
A #columns == B
#rows
A^p
x.*y'
x*y
y*x
28
Grades Exercise
Revisited
Consider the following:
Grade
80
Participation in
class
1.10 1.0
5
77
70
60
65
1.0
7
0.99 1.1
0
Answer:
>> grades = [80 77 70 60 65];
>> participation_in_class
= [1.10,
1.05, 1.07, 0.99, 1.10];
>> final_grades = (grades .* participation_in_class) + 10
final_grades =
98.0000 90.8500 84.9000 69.4000 81.5000
29
String Manipulations
30
s(1:4)
ans = I am
[s([1:9,6,10]) '!']
I am a star!
s(end:-1:1)
ans = gnirts a ma I
31
Tip
num2str
str2num
Are very useful
for output
string
generation
32
>> str_mat =
char('one','two','three')
str_mat =
one
two
Three
Concatenate
vertically!
Rows length must be
equal! (a square matrix)
33
34
Array Editor
35
Display:
Omitting the ;
disp(hello world);
[Today we added num2str() ]
Example:
three=2;
[Oneplusoneis:'num2str(three)]
Array Editor:
37
Ax=b
a21 x1 a22 x2 a23 x3 b2
a x a x a x b
32 2
33 3
3
31 1
A=[1
1
1;1
2
3;1
3
6 ];
b = [3; 1; 4];
x = A\b;
Solution: x = 10
-12 5
More: search help solving linear equations
38
Result:
x=
25.0000
22.0000
99.0000
39
Summary
40