Académique Documents
Professionnel Documents
Culture Documents
http://www.mathworks.com/academia/student_ce
nter/tutorials/launchpad.html
Naming variables
>> x =
5
>> st =
'string'
x=
st =
5
string
>> M = floor(randn(3,3))
M=
dimM =
-1
-2
-2
-1
0
-1
1
0
-2
>> v = [2 4 6 8]
v=
v2 =
2
>> v2 = [2; 4; 6;
8]
2
4
6
8
>>
v(3)
>> v2'
ans =
ans =
2
6
Vectors
>> v = [2; 4; 6;
8]
v=
2
4
6
8
>> v(7) =
10
v=
2
4
6
8
0
0
10
>> v(2) =
15
v=
2
15
6
8
0
0
10
>> v(4) = [ ]
v=
2
15
6
0
0
10
Vectors
>> z = [1:5]
>> y = [1:2:10]
z=
y=
1
>> z2 = [3:8]
>> y2 = [4:.5:6]
z2 =
y2 =
4.0000
4.5000
5.0000
5.5000
6.0000
Vectors
>>
z2(1:3)'
>>
z2(1:2:6)'
>> z2(3:1:1)'
>> z2([1 2 5
6])'
ans =
ans =
ans =
ans =
3
4
5
3
5
7
5
4
3
3
4
7
8
>> y = [1:20:100]
x=
y=
25
10
15
20
21
41
if multiplying or
dividing elements of
one vector by those of
another, be sure to use
. before * or /
61
81
>> compare = x' < y' >> both = y' < 50 & y' == 41
add =
multiply =
compare =
6
31
56
81
106
5
210
615
1220
2025
0
1
1
1
1
both =
0
0
1
0
0
>> z = [1:5]
z=
1
>> y = [1:20:100]
81
21
41
>> m = [z; y]
ans =
m=
62
y=
1
61
83
1
1
2
21
3 4 5
41 61 81
Matrices
matrix = m x n array of numbers, where m is
rows and n is columns
>> M = [2 4 6; 1 3 5; 7 8 9]
>> M(3,2)
M=
ans =
2
1
7
4
3
8
6
5
9
>> M2(1,3)
M2 =
ans =
2
1
7
4
3
8
6
5
9
Matrices
>> M
>> M2 = floor(5.*randn(3))
>> M + M2
M=
M2 =
ans =
1
3
-7
6
5
8
>> 7M(:,1)
9
ans =
2
1
7
-5
3
-1
>> M2(3,:)
ans =
-7
2
2
6
2
3
4
-1
6
11
11
>> M2 <= M
ans =
-1
1
0
Matrices
Can be thought of in terms of vectors
>> x =[1:5]
>> M = [x; y; z]
x=
M=
1
>> y = floor(2.*randn(1,5))
1 2 3 4 5
-2 1 -1 -2 4
100 80 60 40
20
y=
-2
-1
-2
>> z = [100:-20:20]
z=
100
20
80
60
40
2
4
20
A matrix of matrices
called cells in MATLAB, use curly brackets
>> c{2,2}
ans =
-7
2
-1
[3x3 double]
[1x3 double]
[3x3 double ]
[3x3 logical]
>> c = cell(size(M))
>> c{2,2}
c=
ans =
ans =
[]
[]
[]
[]
[]
[]
[]
[]
[]
-6
2
1
-2
4
7
-6
-1
-6
>> c{2,2}(3,2)
c=
ans =
[]
[]
[]
[]
[3x3 double]
[]
[]
[]
[]
-6
2
1
-2 -6
4 -1
99 -6
>> s = struct('type',{'big','little'},'color','red','x',{3
4})
s=
1x2 struct array with fields:
type
color
x
>> s.type
>> s.color
>> s.x
ans =
ans =
ans =
big
red
ans =
ans =
little
red
3
ans =
4
Graphics examples
>> x = 10.*randn(1,1000);
>> hist(x)
Graphics examples
>> M
M=
1 2 3 4 5
-2 1 -1 -2 4
100 80 60 40
20
>> boxplot(M')
>> x = [1:10];
>> y = [10:-1:1];
>> figure
>> subplot(1,2,1), plot(x,y)
>> subplot(1,2,2), plot(x,y.^2)
Useful commands...