Matlab Variables

Variable Definition

    While Matlab employs variables it does not use a Type Definition or a Dimension Statement.

    If it comes across a new variable name it automatically creates it.  In MATLAB workspace at Matlab prompt (>>) Type the following and hit return at end of line
       
Check out the following Code

    a = 1.0; b = 2, c = 3.0; d = 4.0; e = 5.0; (return)
    one = a; two = b; three = c, four = d, five = e; six = pi; (return)
    who
    A = [a b c; d e pi] % This is a 2  X  3  matrix
                        % The semicolon seperates the rows
                        % The space seperates the array elements
    B = A'                 %  B is the transpose of A
    C = A*B                %  A consistent matrix multiplication
    D = A*C       % An inconsistent matrix multiplication  - should see an error
    
    D = B*C
    help inverse  % inverse is not a MATLAB command
    lookfor inverse  % looks for the word inverse in all the files
                     % looks like INV is the command we need
                    % note: in help INV is capitalized - inuse it
                    % must be lower case
    help inv
    Dinv =  inv(D)
    Cinv = inv(C)
    variable type and storage are assigned in  context or on initiation

    variable names are case sensitive

    variable names start with a letter and can contain upto 31 characters which include letters, digits and underscore ( punctuation characters are not allowed)

    To know what is stored in a variable type its name at the command prompt
        At the Matlab prompt :
        
Check out the following Code
 
            D        % should see what is in D
 

    Matlab has built in variable names.  the following are some of the built in variables: ans,  pi,  eps,  flops,  inf,  NaN or nan,  i , j,  nargin,  nargout, realmin, realmaxAvoid using built-in names.

Numbers

    MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers.
        1        -9        +9.0        0.001        99.098765

    Scientific notation uses the letter e to specify a power-of-ten scale factor.
        2.0e-03        1.07e23    -1.732e+03

    Imaginary numbers use either i or j as a suffix.
        1i        -3.1415j            3e5i

    All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite range of roughly 10-308 to 10+308. (This is sometimes operating system dependent).  However all numbers are displayed differently depending on the context and precision

    MATLAB performs all computations in double precision.  The format command described below switches among different display formats.

  Command          Result                                   Example
  format                 Default.                                                Same as short
  format short      5 digit scaled fixed point                 3.1416
  format long       15 digit scaled fixed point               3.14159265358979
  format short e   5 digit floating-point                         3.1416e+00
  format long e    15 digit floating-point                       3.141592653589793e+00
  format short g   Best of 5 digit fixed or floating      3.1416
  format long g    Best of 15 digit fixed or floating     3.14159265358979
  format hex         Hexadecimal                                        400921fb54442d18
  format bank       Fixed dollars and cents                   3.14
  format rat           Ratio of small integers                      355/113
  format +              +,-,                                                          +3.1416
 
  format compact Suppresses excess line feeds.
  format loose     Add line feeds.
 
     Check out the following Code

format compact
aa = 1      % an integer
ab = 1.0         % a real value - note both aa and ab are displayed the same way
format long
aa
ab            % still same display as before
aa = aa * 1.23    % note the long representation and automatic type conversion
ab = ab*1.23    % same
format short

Operators

+        Addition
 -       Subtraction
 *       Multiplication
 /       Division
 \       Left division
       (described in the section on Matrices and Linear Algebra in Using MATLAB)
 ^       Power
 '       Complex conjugate transpose
 ( )    Specify evaluation order
 
The basic entity in MATLAB is an array.  For example :

                the number 1.0 is considered as a   1 x 1 matrix of value 1.0

                     Check out the following Code

who    % lists all your variables
size(A)    % should return matrix dimensions
                % size()  is a Matlab built-in function
SizeB = size(B)    % what happened here ?
                            % SizeB is an array variable which contains the
                            % information returned from using the size(B)
                            % on the right
SizeB(1)            % returns the number of rows of B

AB = [A  B]    % a new Matrix AB by joining A and B
                        % should see an error

AB = [ A B']    % a new 2 X 6  matrix

AB(:,3)            % returns the 3 rd column of AB

AB(2,:)            % returns the  2 nd row of AB

ABStack = [AB ; AB]  % stack Ab on top of itself

ABStack(2,2) = 10.0        % change the 2,2 element to 10.0 from 5.0
 

We will visit Matlab functions next