JCDaly

MATLAB Tutorial

MATLAB "help" is very useful.
In the command window type,
help command
where command is any
MATLAB command.

If you do not know the
command to use, type,

lookfor keyword
where keyword is a word
related to the command.
  1. Arrays
  2. Element by Element Operations
  3. Suppressing Output
  4. Special Arrays
  5. Array Addressing
  6. Loading data from a file

  1. Arrays

    MATLAB variables are arrays of numbers. An array consisting of one element is called a scalar. For example;

    x =2
    x has the value 2. More commonly, x will assume a number of values, say from 0 to 5, For example; x = 1:0.5:5 Now x is an array of numbers;
    x = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
    Note, 1:0.5:5 creates an array of numbers from 1 to five in steps of 0.5.

    Examples of arrays

    A = [1, 2; 3, 4; 5, 6]
    Creates a 3x2 array, 3 rows and 2 columns. The semicolon creates a new row.
            A =  1  2
     
                 3  4
     
                 5  6
     
    
    x = [4; 5]
    Creates an array with 2 rows and one column.
       x = 4
           5
    

    If we multiply A by x, the rules of matrix algebra apply. That is,

     
        A*x = 1*4 + 2*5  =  14
     
              3*4 + 4*5     32
      
              5*4 + 6*5     50
     
    
    A*x is an array with 3 rows and one column.

  2. Element by Element Operations

    Many times matrix multiplication is not desired. For example, when we want the square in each element in an array.

    x = 0:1:4

    y = x .* x

    Note that .* is used to denote element by element operation. The result is;
    x= [0 1 2 3 4]

    y = [0 1 4 9 16]

    Also If stem replaces plot in the above examples, a plot of discrete values represented by a lollipop structure results. For example;
    stem(x,y)
    You can add axis labels and a title to stem plots.

  3. Suppressing Output

    MATLAB echos back all input and values generated. The semicolon, ;, suppresses this output. For example;

    x = 1:.01:10;
    generates an array x with 100 elements, but the array is not echoed back.

  4. Special Arrays

    zeros(4,5) is an array with 4 rows and 5 columns where every element is 0

    ones(2,3) is an array with 2 rows and 3 columns where every element is 1

  5. Array Addressing

    Consider the 3x4 array

     
            A =   1   2   3   4
     
                  5   6   7   9
     
                  0   9   3   1
     
    
    The array has 3 rows and 4 columns. Any individual element can be extracted from the array. For example,
    x = A(2,3);
    results in a value of 7 for x.

    The command

    D = A(:,3);
    produces the colunm array D
            D = 3
            
                7
     
                3
    

    The command

    y = A([1   2],[2   4])
    results in the array,
            y =  2   4
              
                 6   9
    
    y consists of rows 1 and 2 and columns 2 and 4 of the array A.

  6. Loading data from a file

    Consider the file called bipolar with the following content,

    0.63    1.00E-006       1.97E-004
    0.65    2.60E-006       5.19E-004
    0.68    6.50E-006       1.34E-003
    0.7     1.90E-005       3.90E-003
    0.73    5.60E-005       1.16E-002
    0.75    4.97E-004       8.02E-002
    0.78    8.98E-004       1.07E-001
    0.8     1.72E-003       1.32E-001
    
    The MATLAB command load bipolar loads the data from the file bipolar into an array called bipolar.

    The MATLAB command,

    Ib = bipolar(:,2);
    creates a one column array called Ib.
         Ib =  	1.00E-006     
    		2.60E-006    
    		6.50E-006   
    		1.90E-005  
    		5.60E-005 
    		4.97E-004 
    		8.98E-004
    		1.72E-003  
    
    The second column of bipolar is equal to the new one column array Ib.