Computing with MATLAB (Interactive mode)

Online help is available from the MATLAB prompt, both generally (listing all available commands):

>> help
[a long list of help topics follows]

and for specific commands:

>> help lu 
[a help message on the lu function follows].

Vectors

Let's start off by creating a vector. Enter each element of the vector (separated by a space) between brackets, and set it equal to a variable. For example, to create the vector a, enter into the MATLAB command window:

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

MATLAB returns:

     a =
            1  2  3  4  5  6  7  8  9  

To create a vector with elements between 0 and 10 evenly spaced in increments of 2 :

>> t = 0:2:10

     t =
          0  2  4  6  8  10  

You can manipulate vectors very easily. To add a number to each vector entry:

>> b = a + 3

     b =
         4 5 6 7 8 9 10 11 12  

To add vectors:

>> c = a + b 
5     7     9    11    13    15  17    19    21

Subtraction of vectors of the same length works exactly the same way.

Matrices

MATLAB has many types of matrices which are built into the system. A matrix with random entries is produced by typing

rand command within MATLAB:

>> rand(3,4)
ans =

 ans =

  Columns 1 through 3 

    0.5226    0.9797    0.8757
    0.8801    0.2714    0.7373
    0.1730    0.2523    0.1365

  Column 4 

    0.0118
    0.8939
    0.1991




>>help rand

RAND Uniformly distributed random numbers.

    RAND(N) is an N-by-N matrix with random entries, chosen from
    a uniform distribution on the interval (0.0,1.0).
    RAND(M,N) and RAND([M,N]) are M-by-N matrices with random entries.
    RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays.
    RAND with no arguments is a scalar whose value changes each time it
    is referenced.  RAND(SIZE(A)) is the same size as A.  

Some of the standard matrices from linear algebra are :

 
>> zeros(2,3) % Matrix of size 2x3 with zeros 
ans =

     0     0     0
     0     0     0

>> ones(5)    % Square matrix 
ans =

     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1

>> eye(4)     % Identity matrix size 4x4
ans =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

You can also build matrices of your own with any entries that you may want.

>> A= [1  2   3   5   7   9]
A =

     1     2     3     5     7     9

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

     1     2     3
     4     5     6
     7     8     9

>> C = [1  2 
      3  4 
      5  6]
C =

     1     2
     3     4
     5     6

MATLAB syntax is convenient for blocked matrices:

>> D= [B; zeros(3)]
D =

     1     2     3
     4     5     6
     7     8     9
     0     0     0
     0     0     0
     0     0     0

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

>> A(2:3, 1:2)
ans = 
   4 5 
   7 8 

>> B = A(2:3, 1:2)
B = 
  4 5 
  7 8 

>> size(A)
ans = 
  3 3 

>> A
A = 
   1 2 3 
   4 5 6 
   7 8 9 

>> A'
ans = 
   1 4 7 
   2 5 8 
   3 6 9 

>> inv(A)  % Inverse of a A
Warning: Matrix is close to singular or badly scaled. 
Results may be inaccurate. RCOND = 2.055969e-018. 
ans = 
  1.0e+016 * 
  -0.4504   0.9007  -0.4504 
   0.9007  -1.8014   0.9007 
  -0.4504   0.9007  -0.4504 

>> cond(A)    % Condition of A
ans = 
  8.5796e+016 

>> B = eye(3) %Identity matrix
B = 
  1 0 0 
  0 1 0 
  0 0 1 
>> A + B    
ans = 
  2 2 3 
  4 6 6 
  7 8 10 
>> A - B
ans = 
  0 2 3 
  4 4 6 
  7 8 8 
>> A*B
ans = 
  1 2 3 
  4 5 6 
  7 8 9 
>> A(1,3)
ans = 
  3 
>> A(3,3)
ans = 
  9 
>> diag(A)
ans = 
  1 
  5 
  9 
>> norm(A)
ans = 
  16.8481 

>> B = 2*A
B = 
  2  4  6 
  8 10 12 
 14 16 18 
>> B = 3*A;
>> B 
B = 
  3   6  9 
  12 15 18 
  21 24 27 
>> x = 0:.01:1;
>> length(x) 
ans = 
  101 

  

Colon notation

MATLAB offers some powerful methods for creating arrays and for taking them apart.

>> x = A(:,1)
x = 
  1 
  4 
  7 
>> y = A(2,:)
y = 
  4 5 6 
>> x + y'
ans = 
  5 
  9 
  13 

>> %If improper dimensions in operations => error message
>> x + y 
??? Error using ==> + 
Matrix dimensions must agree. 

Summary, the colon notation to select:

a row of A

A(2,:)

a column of A

A(:,3)

part of a row of A

A(2:4,:)

part of a column of A

A(:,3:5)

a block of A

A(2:4,3:5)

some elements of A

A(1:2:5,:)



You can also augment A by putting a vector into a row or column position:

>>A(:,[1 2 ])
     1     2
     4     5 
     7     8
>>A([2 3],[1 2]) 
     4      5
     7      8
>>D=rand(3)
D =

    0.2987    0.4692    0.5828
    0.6614    0.0648    0.4235
    0.2844    0.9883    0.5155

>>D([1 2],:)=A([1 2],:)
    1.0000    2.0000    3.0000
    4.0000    5.0000    6.0000
    0.2844    0.9883    0.5155

>>A(:,[1 3])=D(:,[1 3])
A =

    1.0000    2.0000    3.0000
    4.0000    5.0000    6.0000
    0.2844    8.0000    0.5155

>>A(:,[2 3])=D(:,[2 3])
A =

    1.0000    2.0000    3.0000
    4.0000    5.0000    6.0000
    0.2844    0.9883    0.5155


>>A=D(:,3:-1:1)

A =

    3.0000    2.0000    1.0000
    6.0000    5.0000    4.0000
    0.5155    0.9883    0.2844

>>v=[2 1 3 ]'
v =

     2
     1
     3

>>A(:,v)
ans =

    2.0000    3.0000    1.0000
    5.0000    6.0000    4.0000
    0.9883    0.5155    0.2844


>>A(v,:)
ans =

    6.0000    5.0000    4.0000
    3.0000    2.0000    1.0000
    0.5155    0.9883    0.2844

Built-in functions

MATLAB includes many standard functions such as sin, cos, log, exp, sqrt, as well as many others.

>>sin(pi/4)

ans =

     0.7071
     

To determine the usage of any function, type help [function name] at the MATLAB command window.

>>m = max(A)
m =

     6     5     4

>>max(m)
ans =

     6

Some MATLAB functions can return more than one value. In the case of max the interpreter returns the maximum value and also the column index where the maximum value occurs.

>>[m, i] = max(B)

m =

    21    24    27


i =

     3     3   3  

>> load mydata
>> whos
Name Size Bytes Class 
A 3x3 72 double array 
b 3x1 24 double array 
Grand total is 12 elements using 96 bytes 
>> A
A = 
0.3333 0.2500 0.2000 
0.5000 0.3333 0.2500 
1.0000 0.5000 0.3333 
>> b
b = 
0 
0 
1 
>> x = A \b;
>> x 
x = 
9.0000 
-36.0000 
30.0000 
>> %Error in solution
>> A * x - b 
ans = 
1.0e-015 * 
0 
0.8882 
0 
>> norm(A * x - b)
ans = 
8.8818e-016 
>> rank(A) 
ans = 3
>> cond(A)
ans = 
524.0568 
>> det(A)
ans = 
-4.6296e-004 
>> [L,U] = lu(A)
L = 
  0.3333 1.0000 0 
  0.5000 1.0000 1.0000 
  1.0000 0      0 
U = 
  1.0000 0.5000 0.3333 
  0      0.0833 0.0889 
  0      0     -0.005 6 
>> det(L)*det(U) 
ans = 
-4.6296e-004 

 

MATLAB has a convention in which a dot in front of an operation usually changes the operation. In the case of multiplication, A.*B will perform entry-by-entry multiplication instead of the usual matrix multiplication.

>>A.*B  (there is a dot before the *)

ans =

    1.0000    1.5000    1.8000
    6.0000    5.0000    4.5000
   21.0000   12.0000    9.0000



x = 5

>>x^2
ans =

    25
>>A*A
ans =

    0.4361    0.2667    0.1958
    0.5833    0.3611    0.2667
    0.9167    0.5833    0.4361

>>A^2
ans =

    0.4361    0.2667    0.1958
    0.5833    0.3611    0.2667
    0.9167    0.5833    0.4361
>>A.^2   %(dot)
ans =

    0.1111    0.0625    0.0400
    0.2500    0.1111    0.0625
    1.0000    0.2500    0.1111
>>A
A =

    0.3333    0.2500    0.2000
    0.5000    0.3333    0.2500
    1.0000    0.5000    0.3333

>>triu(A)
         0.3333    0.2500    0.2000
         0         0.3333    0.2500
         0         0         0.3333
>>tril(A)
ans =

    0.3333         0         0
    0.5000    0.3333         0
    1.0000    0.5000    0.3333
>>diag(A)
ans =

    0.3333
    0.3333
    0.3333
>>diag(diag(A))
ans =

    0.3333         0         0
         0    0.3333         0
         0         0    0.3333

>>C=rand(4,5)
C =

  Columns 1 through 3 

    0.3340    0.7604    0.3798
    0.4329    0.5298    0.7833
    0.2259    0.6405    0.6808
    0.5798    0.2091    0.4611

  Columns 4 through 5 

    0.5678    0.0503
    0.7942    0.4154
    0.0592    0.3050
    0.6029    0.8744


>>size(C)
ans =

     4     5

>>[m,n] = size(C)
m =

     4


n =

     5

Relations and Logical Operations

In this section you should think of 1 as "true" and 0 as "false." The notations &, |, ~ stand for "and", "or," and "not", respectively. The notation == is a check for equality.

>>a=[1 0 1 0]
a =
     1     0     1     0

>>b=[1  1  0  0]
b =
     1     1     0     0

>>a==b
ans =
     1     0     0     1

>>a<=b
ans =
     1     1     0     1

>>~a
ans =
       0     1     0     1

>>a&b
ans =
     1     0     0     0

>>a & ~a
ans =
     0     0     0     0

>>a | b
ans =
     1     1     1     0

>>a | ~a
ans =
     1     1     1     1

Order of precedence

Mathematical expressions are evaluated starting from left to right, with the exponentiation operator having the highest order of precedence, followed by multiplication and division (equal precedence), and followed by addition and subtraction (equal precedence). Parentheses can be used to modify the order, starting with the operation in the innermost pair of parentheses and proceeding outward.
Order Operator
1 ( ) starting with the innermost pair
2 ^ from left to right
3 * and / from left to right
4 + and - from left to right

Plotting

To plot first make the vectors you are interested in plotting and then type the plot command.

For example to create a time vector and then compute the sine and cosine value at each time:

>>t=-3*pi:0.1:3*pi;
>>y1 = sin(t);
>>y2 = cos(t);
>>plot(t,y1, t, y2,'r*')
>>title('My plot')
>>xlabel('x')
>>ylabel('cos(x) and sin(x)')


Other Features

Display mode

The MATLAB display only shows 5 digits in the default mode (

although MATLAB always keeps and computes in a double precision

16 decimal places and rounds the display to 4 digits).

The command

>>format long
>>d
d =
   0.50000000000000

will switch to display all 16 digits and

>>format short
>>d
d =

    0.5000

will return to the shorter display. To display in the scientific notation:

>>format  short e
>>d
d =

  5.0000e-001

and 
>>format long e
>>d
d =

    5.000000000000000e-001

To avoid showing results put a semicolon after your commands.

>>diary my_filename.txt

Once a file name has been established you can toggle the diary with the commands

>>diary on

and

>>diary off

This will copy anything which goes to the screen (other than graphics) to the specified file. Since this is an ordinary ASCII file, you can edit it later.

Working with files

MATLAB uses several types of files that allow you to save and retrieve programs, data and variables from a given session: The save and load command are used to save and restore file contents. A very easy way to work with data file is by using the Import Wizard . This is done by selecting the Import Data option on the MATLAB Desktop File menu. The Import Wizard will open a series of windows in order to guide the entire process.

MATLAB demonstrations

MATLAB is shipped with a number of demonstration programs. Try

>>demo 

and

>>matdemo
Some MATLAB function descriptions