TA's note on using Matlab

TA: Hieu Nguyen

Email: htn005@ucsd.edu

Office Hours: 11:00am-12:00pm Thursdays

                       in 5748 AP&M (I might stay in AP&M 2402 after sections until no one has question )

If you are not very experienced with MATLAB I highly recommend you watch the two tutorial videos below

 

A copy of what we did in sections can be found below:

There are also things that I did not have time to cover:

If you need more information try the Matlab tutorial at Stanford

>> %MATLAB=MATrix LABoratory

 

>>% First Look

>>%          Command Window

>>%          Command History

>>%          Workspace

>>%          Current Directory

 

% Creating matrices

 

>> A=[1 2 3; 4 5 6]

 

A =

 

     1     2     3

     4     5     6

 

>> B=[2 3 4; 5 6 7; 7  8 9]

 

B =

 

     2     3     4

     5     6     7

     7     8     9

 

>> C=B(1:2,1:2)

 

C =

 

     2     3

     5     6

 

>> col2=B(:,2)

 

col 2=

 

     3

     6

     8

 

>> row3=B(3,:)

 

row 3=

 

     7     8     9

 

>> C=[1 2 3; 4 5 6; 7 8 9]

 

C =

 

     1     2     3

     4     5     6

     7     8     9

>>% Matrix Operation

>> B+C

 

ans =

 

     3     5     7

     9    11    13

    14    16    18

 

>> B-C

 

ans =

 

     1     1     1

     1     1     1

     0     0     0

 

>> B*C

 

ans =

 

    42    51    60

    78    96   114

   102   126   150

 

>> B^2

 

ans =

 

    47    56    65

    89   107   125

   117   141   165

 

>> B*B

 

ans =

 

    47    56    65

    89   107   125

   117   141   165

 

>> % Element-wise Operations

>> B

 

B =

 

     2     3     4

     5     6     7

     7     8     9

 

>> B.^2

 

ans =

 

     4     9    16

    25    36    49

    49    64    81

 

>> B

 

B =

 

     2     3     4

     5     6     7

     7     8     9

 

>> C

 

C =

 

     1     2     3

     4     5     6

     7     8     9

 

>> B.*C

 

ans =

 

     2     6    12

    20    30    42

    49    64    81

 

>> %  Basic functions

>> A

 

A =

 

     1     2     3

     4     5     6

 

>> size(A)

 

ans =

 

     2     3

 

>> D=transpose(A)

 

D =

 

     1     4

     2     5

     3     6

 

>> size(D)

 

ans =

 

     3     2

 

>> length(A)

 

ans =

 

     3

 

>> A

 

A =

 

     1     2     3

     4     5     6

 

>> D

 

D =

 

     1     4

     2     5

     3     6

 

>> length(D)

 

ans =

 

     3

 

>> D=transpose(A)

 

D =

 

     1     4

     2     5

     3     6

 

>> D=A'

 

D =

 

     1     4

     2     5

     3     6

 

>> det(B)

 

ans =

 

     0

 

>> det(A)

??? Error using ==> det

Matrix must be square.

 

>> ans*A

 

ans =

 

     0     0     0

     0     0     0

 

>> rank(A)

 

ans =

 

     2

 

F =

 

     1     3     4

     5     7     8

     8     9     0

 

>> inv(F)

 

ans =

 

   -0.9474    0.4737   -0.0526

    0.8421   -0.4211    0.1579

   -0.1447    0.1974   -0.1053

 

>> inv(F)*F

 

ans =

 

    1.0000   -0.0000         0

        0    1.0000         0

         0         0    1.0000

 

>> format long

>> inv(F)*F

 

ans =

 

   1.00000000000000  -0.00000000000000                  0

                  0   1.00000000000000                  0

                  0                  0   1.00000000000000

 

>>% Creating M-files

>>Open M-file Editor:

     File->New->M-file

     or Ctrl+N

     or Click on the "blank page" icon

>> To run a M-file

      +) Inside Editor:  Debug-->Run

                             or F5

      +) In Command Window: Type the name of the M-file

The following are different version of a M-file used to compute trace of a square matrix:

>>% Special Matrices

>> eye(3)

 

ans =

 

     1     0     0

     0     1     0

     0     0     1

 

>> ones(3,1)

 

ans =

 

     1

     1

     1

 

>> ones(2,3)

 

ans =

 

     1     1     1

     1     1     1

 

>> zeros(4)

 

ans =

 

     0     0     0     0

     0     0     0     0

     0     0     0     0

     0     0     0     0

 

>> zeros(2,3)

 

ans =

 

     0     0     0

     0     0     0

>>% Controlling and Loops  

>> help for

 FOR    Repeat statements a specific number of times.

    The general form of a FOR statement is:

 

       FOR variable = expr, statement, ..., statement END

 

    The columns of the expression are stored one at a time in

    the variable and then the following statements, up to the

    END, are executed. The expression is often of the form X:Y,

    in which case its columns are simply scalars. Some examples

    (assume N has already been assigned a value).

 

         FOR I = 1:N,

             FOR J = 1:N,

                 A(I,J) = 1/(I+J-1);

             END

         END

 

    FOR S = 1.0: -0.1: 0.0, END steps S with increments of -0.1

    FOR E = EYE(N), ... END  sets E to the unit N-vectors.

 

    Long loops are more memory efficient when the colon expression appears

    in the FOR statement since the index vector is never created.

 

    The BREAK statement can be used to terminate the loop prematurely.

 

    See also if, while, switch, break, continue, end.

 

    Reference page in Help browser

       doc for

 

>> help for

 FOR    Repeat statements a specific number of times.

    The general form of a FOR statement is:

 

       FOR variable = expr, statement, ..., statement END

 

    The columns of the expression are stored one at a time in

    the variable and then the following statements, up to the

    END, are executed. The expression is often of the form X:Y,

    in which case its columns are simply scalars. Some examples

    (assume N has already been assigned a value).

 

         FOR I = 1:N,

             FOR J = 1:N,

                 A(I,J) = 1/(I+J-1);

             END

         END

 

    FOR S = 1.0: -0.1: 0.0, END steps S with increments of -0.1

    FOR E = EYE(N), ... END  sets E to the unit N-vectors.

 

    Long loops are more memory efficient when the colon expression appears

    in the FOR statement since the index vector is never created.

 

    The BREAK statement can be used to terminate the loop prematurely.

 

    See also if, while, switch, break, continue, end.

 

    Reference page in Help browser

       doc for

>> help while

 WHILE  Repeat statements an indefinite number of times.

    The general form of a WHILE statement is:

 

       WHILE expression

         statements

       END

 

    The statements are executed while the real part of the expression

    has all non-zero elements. The expression is usually the result of

    expr rop expr where rop is ==, <, >, <=, >=, or ~=.

 

    The BREAK statement can be used to terminate the loop prematurely.

 

    For example (assuming A already defined):

 

            E = 0*A; F = E + eye(size(E)); N = 1;

            while norm(E+F-E,1) > 0,

               E = E + F;

               F = A*F/N;

               N = N + 1;

            end

 

    See also for, if, switch, break, continue, end.

 

    Reference page in Help browser

       doc while

>> % Handy Stuffs:

+) Use  "Up" or  "Down" Arrow keys to browse previous commands

+) command "clc" clear the Command Windows

+) "^C "(Ctrl +C) terminate a running code (especially useful when you have infinite loop)

+) "format long" (see numbers with more digits after decimal point), use "format short" to go back to standard format