Home » MATLAB Variables

MATLAB Variables

Objective: To study data types, constant and variable, character, operators, assignment statement. To consider vectors and matrices commands to operate on vectors and matrices, matrices manipulation.

Variables: MATLAB variable is a region of memory containing an area which is known by the user-specified name.

These are of two types:

  • double: – 64 bit (10^-308 to 10^308 with 15 to 16 significant decimal digits).
    e.g:  var=10.5  //Real value  var=10i , var=-4j //Imaginary value   var=10+10i
  • char: – 16 bit (-32768-32767)
    e.g:  var='This is a character string';

Initialization of a variable:

  • Assignment statement: It is simplest and used for small arrays.
                Var=expression;
    Where var is name and expression are a scaler constant, an array or a combination of constraints, other variables, and mathematical operations.
    The number of elements in every row must be same. Also, the number of elements in every column must be the same.
            >> var= [3.4]        //creates a 1x1 array with value 3.4                           var = 3.4000                >> var= [1.0 2.0 3.0]  // creates a 1x3 array with value [1 2 3]                            var=1 2 3                  >> var= [1.0;2.0;3.0]  // creates a 3x1 array                              var =    1                                          2                                          3                  >> var= [1,2,3;4,5,6]  //creates a 2x3 array                               var =   1     2     3                                          4     5     6                  >> var= [1,2,3                                 4,5,6]    //creates a 2x3 array                                var =  1     2     3                                            4     5     6                    >> var= []     //creates an empty array                                  var = []                    >> var1= [0 1+7]                                   var1 = 0     8                    >> var2=[var1(2) 7 var1]                                var2 =  8     7     0     8                    >> var3(2,3) =5                                var3 = 0     0     0                                           0     0     5                    >> var3(3,3) =6                                     var3 =0     0     0                           0      0     5                                               0     0     6  
  • Shortcut expressions:
>> x= [1:2:10]          x =    1     3     5     7     9

Shortcuts can be combined with the transpose operator (‘) to initialize column vectors.

               >> f= [1:4]'                          f =1                              2                              3                              4                 >> g=[1:4]                           g =1     2     3     4               >> h=[g',g']                          h =1     1                               2     2                               3     3                           4     4
  • Built in functions:
    Zeros:
                  >> a=zeros (2)                              a =0     0                                   0     0                  >> b=zeros (2,3)                             b = 0     0     0                                   0     0     0                   >> c= [1,2;3,4]                          c =1     2                               3     4                   >> d=zeros(size(c))                            d =     0     0                                    0     0

Ones: 0’s are replaced by 1’s

               >> f=ones (4)                          f =     1     1     1     1                                   1     1     1     1                                   1     1     1     1                                   1     1     1     1

Eyes: to generate arrays containing identity matrices in which all on-diagonal element is 1 while all off-diagonal element is 0.

                >> e=eye(4)                             e =     1     0     0     0                                       0     1     0     0                                       0     0     1     0                                       0     0     0     1
  • Keyboard input:input funcn
    • var=input (‘enter a input value’)
    • enter a input value
    • var=input (‘enter data’,’s’)
    • enter data 3
      var =3
  • Multidimensional array
    • c(:,:,1) =[1 2 3;4 5 6]
    • c =       1       2       3
            4       5       6
    • c(:,:,2) = 7       8       9
            10       11       12
  • STORING MULTIDIMENSIONAL ARRAY IN MEMORY
    •Eg: a= [1 2 3;4 5 6;7 8 9;10 11 12]  a =                 1     2     3                 4     5     6  7 8     9    10    11    12
  • Subarray
    •arr= [1 2 3;-2 -3 -4;3 4 5]              arr =  1     2     3                       -2    -3    -4                         3     4     5  arr1(1,:)=[1 2 3]  •arr1 =     1     2     3
  • End func:
    •arr=[1 2 3 4 5 6 7 8];  •>> arr(5:end)            ans =  5     6     7     8  •>> arr(1:2,[1,4])=[20 21 ;22 23]            arr =  20   2     3    21     5     6     7     8      22     0     0    23     0     0     0     0    •>> arr=[1 2 3 4 ;5 6 7 8;9 10 11 12];  •>> arr(1:2,1:2)=1            arr =       1     1     3     4       1     1     7     8       9    10    11    12
  • Predefined specific values:
    • pi: It contains values up to 15 significant digits
    • inf: infinity
    • nan: not a number
    • clock: current date & time in the form of 6 element vector containing
      year, month, datemin and second
    • date: current date in a character string format eg:24-Jan-2019
    • eps:Esiton i.e smallest difference b/w two no’s
    • ans: It used to store the result of an expression
  • Displaying Output Data:
    • Disp fn: It accepts an array of arguments & shows the value of the array in the command
           window       str=['the value of pi='num2str(pi)];       disp(str);
  • Formatted Output Function With fprintf Function:
    • Display one or more values together with related text & lets the programmer to control the way the display value appears
    • Syntax: fprintf(format,data);
    • Eg:fprintf(‘the value of pi=%fn’,pi);
      fprintf(‘the value of pi=%dn’,pi);
      fprintf(‘the value of pi=%en’,pi);
  • Scalar Operations:
    •E.g.: =2^ ((8+2)/5)           =26(10/5)           =2^2           =4
  • Array Operations:
             z=1                   >> z           z =1     2                3     4    >> y=-1           y = -1         >> y            y =-1     3                 -2      0                      >> y+z  ans =       0     5       1     4
  • Matrix Operation based on Linear Algebra:
Operations Matlab Form Comments
Array
Addition
a+b Array & matrix addition are identical
Array Subtraction a-b Array & matrix subtraction are identical
Array Multiplication a.*b Array multiplication element by element multiplication of a & b
Matrix Multiplication a*b For matrix multiplication no of the column in matrix a=no of column of matrix b
Array Right Division a./b Element by element division of a & b
Array Left Division a.b Element by element division of a & b with in the Nr.
Matrix Right Division a/b a* inv(b) where inv represent inverse
Matrix Left Division ab inv(a)*b
Array Exponential a.^b Element by element exponential of a & b i.e a( i , j) ^ b( i , j)

You may also like