MATLAB Short Course 1
Contents
Basic Numbers
1.3
1
pi
exp(1)
ans =
1.3000
ans =
1
ans =
3.1416
ans =
2.7183
Basic arithmetics
1+2-3
1+2*3
2^3
2^3^3
2^(3^3)
(2^3)^3
ans =
0
ans =
7
ans =
8
ans =
512
ans =
134217728
ans =
512
Logical comparison
1 < 2
2 > 3
ans =
logical
1
ans =
logical
0
machine epsilon (becareful about finite precision
eps
1+eps/2 == 1
1+eps == 1
1e-300 == 0
1e-323 == 0
1e-324 == 0
ans =
2.2204e-16
ans =
logical
1
ans =
logical
0
ans =
logical
0
ans =
logical
0
ans =
logical
1
Use "help" for documentation
log(2)
help log
sin(30)
help sin
ans =
0.6931
LOG Natural logarithm.
LOG(X) is the natural logarithm of the elements of X.
Complex results are produced if X is not positive.
See also LOG1P, LOG2, LOG10, EXP, LOGM, REALLOG.
Documentation for log
doc log
Other uses of log
codistributed/log quaternion/log
comm/log sym/log
dlarray/log symbolic/log
gpuArray/log tabular/log
matlab.unittest.fixtures.Fixture/log waveletScattering/log
matlab.unittest.TestCase/log waveletScattering2/log
ans =
-0.9880
SIN Sine of argument in radians.
SIN(X) is the sine of the elements of X.
See also ASIN, SIND, SINPI.
Documentation for sin
doc sin
Other uses of sin
codistributed/sin fixedpoint/sin sym/sin tabular/sin
dlarray/sin gpuArray/sin symbolic/sin
Vectors
a = [1 2 3]
b = [1; 2; 3]
a =
1 2 3
b =
1
2
3
common functions with vectors
length(a)
norm(a)
norm(a)
norm(a,'inf')
ans =
3
ans =
3.7417
ans =
3.7417
ans =
3
the use of colon ":"
a = 1:10
a = 1:3.5
a = 3:2:20
a =
1 2 3 4 5 6 7 8 9 10
a =
1 2 3
a =
3 5 7 9 11 13 15 17 19
modify the elements vi indices (start from 1, not zero
a(2) =15
a([2 4 6 8]) = 0
a(2:2:8) = 1
a(2:2:end) = 2
a =
3 15 7 9 11 13 15 17 19
a =
3 0 7 0 11 0 15 0 19
a =
3 1 7 1 11 1 15 1 19
a =
3 2 7 2 11 2 15 2 19
Special matrices
A = [1 2; 3 4]
eye(3)
zeros(2,5)
A =
1 2
3 4
ans =
1 0 0
0 1 0
0 0 1
ans =
0 0 0 0 0
0 0 0 0 0
reshape the matrix (vector)
A = 1:9
reshape(A,3,3)
transpose(reshape(A,3,3))
reshape(A,3,3)'
A =
1 2 3 4 5 6 7 8 9
ans =
1 4 7
2 5 8
3 6 9
ans =
1 2 3
4 5 6
7 8 9
ans =
1 2 3
4 5 6
7 8 9
Random numbers
A = rand(3,3)
A = randn(3,3)
b = rand(3,1)
A =
0.0357 0.6787 0.3922
0.8491 0.7577 0.6555
0.9340 0.7431 0.1712
A =
0.8884 -0.8095 0.3252
-1.1471 -2.9443 -0.7549
-1.0689 1.4384 1.3703
b =
0.0344
0.4387
0.3816
Different ways to solve the linear system Ax=b
x = A\b
x = inv(A)*b
x = linsolve(A,b)
x =
-0.1947
-0.1445
0.2782
x =
-0.1947
-0.1445
0.2782
x =
-0.1947
-0.1445
0.2782