Notes
Outline
The MatLab Language
Mathematics Laboratory
The MathWorks
www.mathworks.com
General
Comments – “%” in first column
Result Control - “;” at the end
"x=5"
x =
5
"x=7;“
"x"
x =
7
General (continued)
Range Operator
"x=3:2:13"
x =
3     5     7     9    11    13
Line Continuation - three periods
Directory
"dir"
announ~2.doc mathview.doc matlab_6.doc matlab~2.doc
intro.m matlab.doc matlab~1.doc vector.m announ~1.doc language.m   matlab1.doc  matlab~1.ppt
General (continued)
Print Working Directory
"pwd"
ans =
E:\DOC\JEFF\COURSES\BEI\SHORTC~1\MATLAB
Change Directory - cd
"cd .."
"pwd"
ans =
E:\DOC\JEFF\COURSES\BEI\SHORTC~1
General (continued)
"cd matlab"
"pwd"
ans =
E:\DOC\JEFF\COURSES\BEI\SHORTC~1\MATLAB
Scalars, Vectors, and Matrices
Creating a row vector
"X=[1,2,3] or X=[1 2 3]
X =
1     2     3
Creating a column vector
"Y=[1;2;3]" or "Y=[1
                                2
                                3]"
Y =
1
2
3
Vector / Matrix Operations
"Z=[y,2*y,3*y]" creates ?
Z =
1     2     3
2     4     6
3     6     9
Indexing
"Z(2,3)=?"
ans =
6
"W=Z(1:2,2:3)"
W =
2     3
4     6
 "W=Z(:,2)"
W =
2
4
6
Indexing (Continued)
"W=Z(2,:)"
W =
2     4     6
"Z(2,:)=[6,4,2] does ?"
Z =
1     2     3
6     4     2
3     6     9
"Z(2,:)=[ ] does ?"
Z =
1     2     3
3     6     9
Matrix Operations
A+B  - Addition, element by element
"ZZ=[1,1,1;2,2,2]"
ZZ =
1     1     1
2     2     2
"W=Z+ZZ"
W =
2     3     4
5     8    11
Matrix Operations (Continued)
A'   - Transpose
A*B  - Matrix Multiplication
"ZZ=ZZ' "
ZZ =
1     2
1     2
1     2
"W=Z*ZZ"
W =
6    12
18    36
Matrix Operations (Continued)
A.*B - Element by Element Multiply
Also: A/B, A\B, A./B, A.^2
Reserved Symbols
Scalars: pi, i, j, inf, NaN, clock, date, ans
"x=pi"
x =
3.1416
"x=i"
x =
0 + 1.0000i
Scalars (Continued)
"x=j"
x =
0 + 1.0000i
Matrices: zeros, ones, eye
"zeros(3,5)"
ans =
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
Matrices (Continued)
"ones(5,3)"
ans =
1     1     1
1     1     1
1     1     1
1     1     1
1     1     1
Matrices (Continued)
"eye(4)" - the identity matrix
ans =
1     0     0     0
0     1     0     0
0     0     1     0
0     0     0     1
Relational Operators
"<"  - less than
"<=" - less than or equal to
">"  - greater than
">=" - greater than or equal to
"==" - test for equality
"~=" - not equal to
"&"  - AND
"|"  - OR
"~"  - NOT
Control Flow (If)
if x>5 & x<8
-------
-------
elseif(x>=8)
-------
-------
else
-------
end
Control Flow (For Loops)
for k=7:21
-------
-------
end
Control Flow (While Loops)
k=1;
while k=<10
-------
-------
k=k+1;
end