Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Tutorial 2 and 3:
Introduction to Matlab
& Data Analysis
Goals
Variables
Read it
Use it for computation / change its value
and save it back to memory
1 1 0 0 0 1 0 1
Variables
clear;
clear all;
2. Case sensitive!!!
3. Cant be a keyword
Underscore
clear max_grade;
Digits
Assignment
operator
Value
Letters
max_grade = 100;
Variable Assignment
Exercise 2:
Exercise 1:
Variable Hands On
Example:
load(file_name);
clear x;
save(file_name, var1, var2, )
which(linspace);
Workspace functions
Data Types
Next lectures
A scalar is also an
array of size 1x1
Default
Integer
10
Real(/Complex):
x = double(235.5);
x = single(235.5);
x = 235;
a = int16(100);
Be careful of
Memory overflow
b = int8(5000);
Integer:
11
Is Not A
Number
Notice:
Infinite
12
Arrays Manipulation
13
a = [1 2 3 4];
b = [1, 2, 3, 4];
c = [1; 2; 3; 4];
d =1:1:4;
e = (1:4);
Creating arrays
14
Min value
Max value
linspace(0,100,51);
Number of points
logspace(1,10,11);
Same, but logarithmically spaced
between 10^1 and 10^10
15
y = sin(x);
z = cos(x);
x = linspace(-pi,pi, 21);
% another way: x = (-1:0.1:1) * pi;
16
[,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,]
Reminder:linspace
Use the commands we learned to define x as:
ans = 3 3
See also: length(A) numel(A)
size(A)
A(2,3) <->A(8)
ans = 8
sub2ind(size(A), 2,3)
B(:, :, 1) = A;
B(:, :, 2) = A;
B(:, :, 3) = A;
1
4
7
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
2
5
8
3
6
9
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
Third dimension
(Page)
A=
1
First dimension
4
(Row)
7
17
Second dimension
(Column)
ans =
2
1
1
3
4
2
ans =
2
4
ans =
3
1
b = fliplr(a)
c = flipud(a)
2
4
a(1,end:-1:1)
a(2)
a(:)
a % or a.
ans=
1
2
3
4
ans=
1
3
2
4
a = [1, 2; 3, 4]
Array flattening
a=
1
3
Arrays Manipulation
ans =
3
18
ans =
1
ans =
2 4
2 4
reshape(a,1,4)
repmat(e,2,2)
g=
1
g = [d(1:3), e]
f=
e=
2
3
4
Horizontal Array
Concatenation
Vertical Array
Concatenation
d=
1
1
2
f = [d; e]
e = 2:2:10
d = 1:2:9
6
6
5
6
9
10
10
8 10 2
8 10 2
7
8
4
4
6
6
8
8
10
10
10
19
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A=
1
2
3
4
5
6
7
8
9
>> A(3,4)
??? Index exceeds matrix dimensions.
>> A(4,4) = 10
A=
1
2
3
0
4
5
6
0
Notice the
7
8
9
0
difference!
0
0
0 10
1
4
7
0
2
5
8
0
3 0
6 0
9 0
0 10
1 2 3
4 5 6
7 8 9 ?
20
Subscripted assignment
dimension must match!
Scalar expansion
2
5
8
3
6
9
21 22 23
24 25 26
1
Notice the 4
difference! 7
>> A(1:2,1:3) = 10
A=
10 10 10
10 10 10
7
8
9
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A=
1
2
3
4
5
6
7
8
9
>> A(:) = 21:30
??? In an assignment A(:) = B, the
number of elements in A and B
must be the same.
21
21
21
4
21
21
10
73
10
3
8
10
18
10
22
21
45
21
21
21
zeros(10,10)
ones(10,10)
rand(10,10)
randn(10,10)
eye(10,10)
1
0
0
0
1
0
0
0
1
Identity matrix
Create a sample.
Allocate memory in
advance!
Standard Arrays
23
>> A(is_larger_than5)
ans =
8
Array can be
9
indexed with
6
logical array!
7
>> class(is_larger_than5)
ans =
Logical
operator
>> A = magic(3)
A=
8 1 6
3 5 7
Logical
4 9 2
1
2
c=
1
2
3
3
the # of output
parameters!
24
What is wrong?
mean(A(4,:))
A = A(:,4:6); % or
A(:,1:3) = [];
A = magic(6)
A(:,:) = ones(7,7)
??? Subscripted assignment dimension mismatch.
Short Example
25
26
A+B
A-B
-A
A.*B
A./B
A.\B
.^
Matrices dimensions
must match
+ * /\ by scalar are
element-wise
Element by element:
x .* y'
x*y
y*x
x = 1:3;
y = x';
27
A #columns == B #rows
A^p
C = A*B :
Array arithmetic
70
60
77
1.10
65
Answer:
1.10
Participation in class
80
Grade
28
String Manipulations
29
I am a star!
ans = gnirts a ma I
[s([1:9,6,10]) '!']
s(end:-1:1)
30
s = I am a string
ans = I am
>> class(d)
ans = double
>> d = str2num(c)
d = 3.1416
>> class(c)
ans = char
>> c = num2str(3.14157)
c = 3.1416
str2num
num2str
Tip
31
>> size(str_mat)
ans = 3
5
>> class(str_mat)
ans = char
Concatenate vertically!
>> str_mat =
char('one','two','three')
str_mat =
one
two
Three
32
>> strcmp('abcd','abc')
ans =
0
>> strncmp('abcd','abc',3)
ans =
1
33
Array Editor
34
two = 2;
[One plus one is:' num2str(two)]
Omitting the ;
disp(helo world);
[Today we added num2str() ]
Example:
Array Editor:
Display:
35
36
a13 x3 = b1
a23 x3 = b2
a33 x3 = b3
a12 x2
a22 x2
a32 x2
Ax=b
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 37
a11 x1
a21 x1
a x
31 1
x = A\b
Answer:
A = [1 2 3
456
7 8 0];
b = [366; 804; 351];
7x+8y = 351
4x+5y+6z= 804
1x+2y+3z = 366
25.0000
22.0000
99.0000
x=
Result:
38
Summary
39