Arrays, vectors, matrices and tables

Arrays

An array is an ordered collection of numbers. Each number can be accessed directly using an index (much like the subscript notation used in mathematics to refer to an element of a vector or a matrix). An array is entered into MATLAB as a list of numbers e.g.

x = [1,3,5,7,9]
or instead of using commas we can use blanks
x = [1 3 5 7 9]

This creates a variable called x which has elements x(1), x(2), ..., X(5). Each such element is a real number and can be used directly. To access the third element we type:

....
z = x(3)
....

This code would result in the variable z assuming a value of 5.

Arrays become useful when one wants to perform the same action on every element of an array. For example, supposed we have an array describing the flows of a set of four components in a mixture. We can find out the total flow by simply adding up the individual flows. One way would obviously be to write a statement of the form

....
  Totalflow = x(1) + x(2) + x(3) + x(4);
....
Though, luckily, MATLAB has a built in function sum which can be used in this instance. We write
....
  Totalflow = sum(x);
....

Vectors

The elements of a horizontal array may be used to represent a row vector. Consequently it may be necessary to define a column vector or vertical array.

A column vector is entered into MATLAB as a list of numbers separated by semi-colons e.g.

y = [2;4;6;8;10]
 y =
    2
    4
    6
    8
    10

Matrices

Thus far we have only introduced one dimensional arrays, however many engineering and mathematical calculations require the use of matrices, which are two dimensional arrays. These are entered in a similar way

>> A = [1 2;3 4]
 A = 
     1 2
     3 4
We can determine the size of any matrix, using the size function
>> size(A)
where the output is
ans =
      2 2

This tells us that A is a matrix with two rows and two columns. Actually, all variable types in MATLAB (scalar variables, and row and column arrays) are in fact matrices of different sizes. So if we determine the size of our row vector, x

>> size(x)
we are told
ans =
      1 5
So x is a matrix 1 row by 5 columns.

Tables

Matrices also provide the easiest way to present data in tabular form. For example we may want to tabulate data showing the relationship between speed and journey time:
 speed = [20 30 40 50 60 70]
 time  = [5 3.3333 2.5 2 1.6666 1.4286]
 
A simple program that produces two alternate tables from this data is shown below. From the program you should note how to transpose vectors and matrices using the special ' operator. This is particularly useful when you want to convert row vectors into column vectors. The program also illustrates how to merge vectors (of the same size) into a large matrix.
% Create table - data in rows
row1 = speed;       
row2 = time;
Table1=[row1;row2]  % Form a matrix from two row vectors 

% Create table - data in columns
col1 = speed';      % ' is used to transpose data from
col2 = time';       %   row vector to a column vector
Table2=[col1 col2]  % Form a matrix from two column vectors 

%End of program
 

The output from the program is

Table1 =

   20.0000   30.0000   40.0000   50.0000   60.0000   70.0000
    5.0000    3.3333    2.5000    2.0000    1.6666    1.4286


Table2 =

   20.0000    5.0000
   30.0000    3.3333
   40.0000    2.5000
   50.0000    2.0000
   60.0000    1.6666
   70.0000    1.4286

Click HERE for next section.