MATLAB Short Course 1

Contents

Basic Numbers

% floaing numbers
1.3

% integer, but saved as floating number
1

% special constant pi = 3.1415926.... (format long for more digits)
pi

% special constant, e = 2.1718.... base of the natural logarithm
exp(1)
ans =

    1.3000


ans =

     1


ans =

    3.1416


ans =

    2.7183

Basic arithmetics

1+2-3

% multiplication/division has higher precedance than addition/subtraction
1+2*3

% power
2^3

% use parenthese to reduce ambiguity
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  % true (output "1" any non-zero number is "true")
2 > 3  % false (output "0")
ans =

  logical

   1


ans =

  logical

   0

machine epsilon (becareful about finite precision

eps
1+eps/2 == 1  % true, but mathematically false
1+eps == 1    % false
1e-300 == 0
1e-323 == 0   % could be different answer on different computers
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)   % which base is used here, 2, 10 or e?
help log % find the base from the help file

sin(30)   % is "1" here degree or radian?
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]    % row vector
b = [1; 2; 3]  % column vector
a =

     1     2     3


b =

     1
     2
     3

common functions with vectors

length(a)
norm(a)    % Euclidean norm
norm(a)    % sum of absolute values
norm(a,'inf')  % maximum of the absolute value
ans =

     3


ans =

    3.7417


ans =

    3.7417


ans =

     3

the use of colon ":"

a = 1:10
a = 1:3.5   % what if the end point is not an integer?
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  % change the second element
a([2 4 6 8]) = 0
a(2:2:8) = 1
a(2:2:end) = 2  % all even indexed elements to 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)'   % ' for transpose
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)    % uniform 3x3
A = randn(3,3)   % standard normal 3x3
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