Department of Chemical Engineering
MATLAB Tutorial
 

 Array (Matrix) Operations                                                             (last updated 9/9/99)

The information in this tutorial is located in the MATLAB manual. Any section number references refer to it.

The Student Edition of MATLAB, Version 5, The MATH WORKS Inc. Prentice-Hall, 1997.

======================================================================================

The following sections are included in this tutoroal;

Introduction

Constructing Arrays

Terminology, Array Referencing, and Transposing

Scalar Array Mathematics

Element-by-Element, Array-Array Mathematics

Array-Array Mathematics

Checking Arrays Stored in Memory

======================================================================================

  Introduction

In the majority of programming applications you will encounter, information is stored or provided to in the form of arrays. Arrays provide a convenient way to store information for calculations, printing, plottting, etc.  There are many times when you want to perform the same operation on multiple numbers. Other times we want to do a calculation repeatedly and save the intermediate values. There are some built-in MATLAB files which require the information in this form. This tutorial will explain the basics of creating, manipulating and performing calculations with arrays.

======================================================================================

Constructing Arrays  -  Section 6.3

There are a number of ways to construct arrays:

Entering the values directly.

>> x = [0 0.1*pi 0.2*pi 0.3*pi 0.4*pi]
x =
    0    0.3142    0.6283    0.9425    1.2566

This is a (1x5) array. Note that mathematical operations can be performed while addessing the elements.

Each row of an array can be ended by a semi-colon.
>> x = [0; 0.1*pi; 0.2*pi; 0.3*pi; 0.4*pi]
x =
    0
    0.3142
    0.6283
    0.9425
    1.2566

This is a (5x1) array.

Rows can also be ended by using a return. This only works with the square bracket notation, not with a parenthesis. This can be a good way of creating large matrices, because the columns can be lined up for easy readability. To create a (3x3) array:

>> y = [ 0     4     7
             3     6     9
             2     4     6 ]
y =
    0   4   7
    3   6   9
    2   4   6
 

Equally Spaced Arrays
Typing every entry is time consuming for long arrays. If the points are equally spaced this isn't necessary. An alternative method is to use the colon notation (first_value: interval: last_value).

>> x = [0: 0.1*pi: 0.4*pi]
x =
    0   0.3142   0.6283   0.9425   1.2566

You could also enter this as:

>> x = [0: 0.1: 0.4]*pi
x =
 0   0.3142   0.6283   0.9425   1.2566

Parenthesis could also be used instead of brackets for this operation, but is not recommended because they are often not interchangable.

If the interval is left out MATLAB uses a default interval of 1.

>> d = 2:8
d =
    2   3   4   5   6   7   8

Another way of specifying an array is to use the linspace command. This allows you to specify the number of data points, but not the interval is to use. The notation is linspace(first_value, last_value, number_of_values):

>> x = linspace(0, 0.4*pi, 5)
x =
    0   0.3142   0.6283   0.9425   1.2566

Arrays can be combined.

>> a = [0 1 2], b = [4 6 8]
a =
    0   1   2
b =
    4   6   8

>> c = [a b]
c =
    0   1   2   4   6   8
 
======================================================================================

Terminology, Array Referencing, and Transposing   -  Section 6.2, 6.4

MATLAB uses the term array to indicate any set of information that is stored with a single variable name. In mathematics we typically speak of vectors or matrices. A vector is simply an array that has either a single row or column. A matrix can be of any size in terms of the rows or columns. We speak in terms of rows and columns, and indicate the size of arrays by specifying the (rows x columns). Six elements might be in any of the following forms: (1x6, 6x1, 2x3, 3x2)

Note that in a vector, individual array elements are normally referenced with a single numerical subscript. In matrices the reference is normally (row,column). These could also be referenced with a single number. 'a(2,2)' in the third matrix above could be accessed by typing 'a(4)'.

Assigning and Addressing Array Elements

Individual array elements can be accessed and used in calculations.

>> x(2)
ans =
    0.3142

>>  y(2,3)
ans =
       9

Sections of an array can be accessed.

>> x(2:4)
ans =
     0.3142   0.6283   0.9425

They can also be accessed in reverse order.

>> x(4:-1:2)
ans =
    0.9425   0.6283   0.3142

If a value is assigned to a specific array element, any elements before that one which have not specifically been assigned a value, are given a default value of zero.

>>  f(4) = 12
>> f
f  =
    0      0      0      12

Transposing

The orientation of an array can be transposed by use of the single quote notation.

>> a = [1 2 3; 4 5 6]
a =
    1   2   3
    4   5   6

This is a (2x3) array.

>> b = a'
b =
    1   4
    2   5
    3   6

This is the transpose of 'a', and is a (3x2) array. The first row of 'a' becomes the first column of 'b', and so forth..
 
======================================================================================

Scalar-Array Mathematics- Section 6.4

Addition, subtraction, multiplication, and division of an array by a scalar applies the operation to all elements of the array.

>> 3*b
ans =
    12   18   24

>> 4*b - 2
ans =
    14   22   30

The order of precedence still hold in terms of the order of calculations.
 
======================================================================================

Element-by-Element, Array-Array Mathematics - Section 6.4

We need to be careful when we are performing array-array calculations. Addition and subtraction of arrays or matrices only exist on an element-by element basis. This means the operation is performed between corresponding elements of the arrays. To do this the arrays must be of the same size and orientation.

>> a, b
a =
    0   1   2
b =
    4   6   8

Addition and subtraction merely use the + and - signs.

>> c = a + b
c =
    4   7   10

For element by element multiplication and division the notation needs to be changed because there are other types of matrix multiplication and division. The notation for element-by-element operations uses a (.* or ./).

>> c = a.*b
c =
    0   6   16

Powers on an element by element basis are dealt with the same way (.^).

>> c = a.^3
c =
      0    1    8

>> c = 2.^a
c =
    1     2     4

To understand what it means to multiply or divide arrays not on an element-by-element basis see a linear algebra text.
 
======================================================================================

Array-Array Mathematics

The description of what the result of multiplying and dividing arrays is left to any text on linear algebra. In MATLAB you multiply arrays with the * operator. The order is important as shown here.   The number of rows of the second matrix, must equal the number of columns of the first.

>> A = [ 1    3
               2    5];

>> B = [ -1    3
               1     4
               3   -2];

>> C = A*B

??? Error using ==> *

Inner matrix dimensions must agree.
 
>> C = B*A
C =
     5      12
     9      23
     -1     -1
 

When dividing arrays the forward and backwards division operators, / or \ , are not the same. Be sure yoiu are using the appropriate one for the operation you are completing.
 
======================================================================================

Checking Arrays Stored in Memory - Section 6.9

Whereas using the 'who'command gives a list of variables, the 'whos' command gives the additional information of the array dimensions. For example:

>> whos
Name Size Bytes Class
a 2 by 3 48 double array
b 3 by 2 48 double array
Grand total is 12 elements using 96 bytes