Unit#8  MATLAB 1 - Engineering Applications
Course Outline Return to Unit
Entering / constructing / displaying arrays

As you read this material we strongly recommend that you activate your MATLAB window and try the commands right there and then


Once you have activated the command window of MATLAB you can start   declaring variables and entering data.  The declaration part is almost trivial in MATLAB.  Everything is assumed to be an array with dynamic dimensions and dynamic word length.  In other words, if we write: length = 3 , this makes the variable length a 1X1 array ( a single number).  If, however we write scores = [ 34, 56, 78 ]   , then scores becomes automatically a 1X3 array ( one row , three columns). Similarly if we write

temperature = [

45,

67,

87

 

35,

51,

45

 

78,

40,

67 ]

or equivalently  temperature = [ 45, 67, 87; 35, 51, 45; 78, 40, 67], then temperature becomes a 3X3 array.  These concepts are illustrated in the picture below:

 

You must open your MATLAB window and repeat the above sequence.  You will notice that the system automatically responds ( to the return button) by printing out the name and value of the variable just entered.  One can prevent this immediate display of the values by placing a semicolon at the end of the definition statement. Of course, at any point we may print, on demand, any variable we please by simply typing its name without a semicolon. See the picture below:

Another important point to notice is this: the whos statement provides a summary of all variables in use by the program. Note that in our example above all numbers are treated as doubles, even though they were entered as integers and we did not specifically ask for double representation.  This “generosity” in memory space makes possible the flexibility of MATLAB when it comes to declarations.  Everything is treated with the same deep detail.  However the output is controlled.  We can show as much or as little detail as we please.  By default we get integers outputted as integers and real numbers by precision 4.  To see this, enter in your MATLAB window a statement like :  b = length / 7.1

We can refer to the individual elements of the arrays using indexes.  For example if I need to change the element of temperature which seats at the second row and third column from 45 to 46.77, I can simply write  temperature(2, 3) = 46.77. Also we can construct arrays using previously defined arrays.  These concepts are shown in the picture below:

Notice that   making a single element of the array temperature a real number, forces the entire array to be displayed in default format for real numbers.

The colon operator and the construction of arrays:

We saw that an array can be constructed from previously defined arrays.  Another mechanism for easy construction of an array is the colon operator ( : ) .  If we write

a = 2.3 : 0.1 : 2.9

a row  array a will be generated which has as first element 2.3 as last element 2.9 and several elements in between so that the increase is 0.1.  In other words we will get  a = [ 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9].   See the example below, and notice what happens if the numbers are not round

We can also use the same device without the specific increase amount .  Then by default the increment is taken to be one.

See again the example below:

As an extension of this, we can use the  colon operator on array indices, so that we can  indicate certain subsets of defined arrays.  See the example below:

 
 

Jacob Y. Kazakia © 2001 All rights reserved